c# - Unity 2D Collision Detection -
i've started working on first 2d game in unity, i'm stuck collision detection problem. there easy way of getting collision detection sides? object has rigidbody2d , boxcollider2d.
the unity oncollisionenter2d method gives reference collider has come in contact gameobject. therefore, can compare position of gameobject position of gameobject has hit you. example:
void oncollisionenter2d(collision2d coll) { vector3 collposition = coll.transform.position; if(collposition.y > transform.position.y) { debug.log("the object hit me above me!"); } else { debug.log("the object hit me below me!"); } if (collposition.x > transform.position.x) { debug.log ("the object hit me right!"); } else { debug.log("the object hit me left!"); } }
Comments
Post a Comment