Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is there a way to determine which face of a collider the collision occurs on?

Discussion in 'Physics' started by TGKG, Jan 29, 2015.

  1. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    I have a cube (like a dice) which collides with another object. Is there a way to determine which face of the dice the collision has occurred on?

    Follow up question:
    Is there a way to determine where on the collider the collision has occured (ie how far away from the center of the face)?
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The Collision structure that gets passed to OnCollisionEnter, etc, has an array of ContactPoints that report the points where the incoming collider made contact. Unfortunately, the contact point only gives the position in space and not the mesh face. However, if the object is a simple cube then you may be able to figure out the face with a bit of vector arithmetic. You can use transform.InverseTransformPoint to get the location of the collision in the cube's local space. From that, you could see which axis of the local point has the greatest absolute value and determine the face from that (eg, if the positive X coordinate is greater than the other two then the hit occurred on the right hand face, etc). You can also use Vector3.Distance to measure the distance from the contact point to the centre of the face.

    Of course, if you start using a mesh that is more detailed than a cube then things could get a bit more complicated ;-)
     
  3. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    for determining the up-face of a die, i think it would be much easier to measure the angular velocity to see if it has finished rolling yet, and then just check its resting orientation to figure out which face is up.
     
  4. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    Wow! Thanks andeeeee and Cpt Chuckles,
    Both those answers are great. I did not know about transform.InverseTransformPoint and never thought of using the angular velocity.
    Thanks, I will try each of those answers.