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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -