- Home /
Enemy and player collision isn't working
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);
}
}
}
Comment
Answer by Neo1923 · May 01, 2021 at 11:27 AM
Thanks but i solved the problem. I did very stupid mistake sorry for bother.
Enemy script was in the Enemy object and box collider was in the Sprite object so they couldn't communicate. :')
screenshot-5.png
(2.6 kB)
Glad you solved it! I removed my reply, because it was actually wrong. Happy coding :)