I am very new to Unity and C# and I was following a guide to learn som more about Unity, but I got stuck because the collision doesn't work. I want the "Player" to get removed when it tocuhes the "Enemy" but when they touch nothing happens, I let Unity use the screwdriver tool to help fix it, but now the collision and the rest of the code doesn't work. Please help I have been stuck for quite a while now.
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public Rigidbody2D rb;
private Vector2 moveDirection;
private void Start()
{
//Destroy(gameObject, 3f);
}
void Update()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");
moveDirection = new Vector2(moveX, moveY).normalized;
}
void FixedUpdate()
{
rb.velocity = new Vector2(moveDirection.x * moveSpeed, moveDirection.y * moveSpeed);
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
Destroy(gameObject);
}
}
↧