How to find which side of a collider has been hit from hit.normal?

14,823
function OnCollisionEnter(collision : Collision)
{
    var relativePosition = transform.InverseTransformPoint(collision.contacts);

    if(relativePosition.x > 0) 
    {
        print(“The object is to the right”);
    } 
    else 
    {
        print(“The object is to the left”);
    }

    if(relativePosition.y > 0) 
    {
        print(“The object is above.”);
    } 
    else 
    {
        print(“The object is below.”);
    }

    if(relativePosition.z > 0) 
    {
        print(“The object is in front.”);
    } 
    else 
    {
        print(“The object is behind.”);
    }
}
Share:
14,823
SpeedBirdNine
Author by

SpeedBirdNine

I am a software engineer and hobbyist gamer. Other than that I like reading and exploring new technologies. Me on Twitter My favorite questions: What is the single most influential book every programmer should read? Why is subtracting these two times (in 1927) giving a strange result? MVVM: Tutorial from start to finish? What is the worst gotcha in C# or .NET? Learning game programming byte + byte = int… why? Fastest sort of fixed length 6 int array Where do you go to tickle your brain (to get programming challenges)? What modern C++ libraries should be in my toolbox? Why is my program slow when looping over exactly 8192 elements? Learning to Write a Compiler

Updated on June 05, 2022

Comments

  • SpeedBirdNine
    SpeedBirdNine almost 2 years

    In Unity3d, i can get the normal of the surface with which the collider collides using hit.normal, but is there a way to find which side has been hit what is provided bu Unity3d?

    One solution is to see the orientation of the normal, and should work well for static objects but what about dynamic and moving objects whose orientation changes?