I have a simple script for enemy movement and collision among player and enemy. Enemy movement is working but collision isn’t working. as so;
{
public float speed;
private Transform target;
// Start is called before the first frame update
void Start()
{
target = GameObject.FindGameObjectWithTag("player").GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("player"))
{
Debug.Log("hit");
Destroy(gameObject);
}
}
}