- Home /
Question by
zaid-abdalkarim · Sep 19, 2017 at 03:31 AM ·
collisionraycastcollidercollidersraycasthit2d
OnCollisionEnter2D is not working
Hello, so i'm making a top down tower defense game and i have been having a problem deleting an object that collides with the road after mouse is up. I've tried Raycasts but the result was that there was a delay and I didn't know how to fix it. here is the script. Thanks to anyone who responds.
Camera maincamera;
bool done = true;
bool mouseup;
void Start()
{
maincamera = Camera.main;
}
private void OnMouseExit()
{
mouseup = true;
}
private void Update()
{
if(done == false)
{
Destroy(this.gameObject);
}
}
void OnMouseDrag()
{
if(done == false)
{
Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
Vector3 objPosition = maincamera.ScreenToWorldPoint(mousePosition);
this.transform.localPosition = objPosition;
}
else if (done == true)
{
return;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (mouseup == true)
{
if (collision.transform.gameObject.name == "road")
{
done = false;
}
else
{
done = true;
}
}
}
Comment