Is it possible to check the direction of which an object had collided into the other?

I want to check the direction of which an object is colliding into other. Is this possible?

I have two gameobjects, a Box and a Stone. Both has a collider with IsTrigger checked. The Stone has a rigidbody. I have a script on the Box that has an OnTriggerEnter function. This function is called when the Stone hits/collides the Box.

Now, I want to know if the Stone actually fell from the top and hit the Box. If the Stone hit the Box from the top, ie, the Stone fell from the top and collided on the top of the Box, I want it to do something. If it the Stone hit the Box from the sides, then it could do another thing.

Can colliders check for the direction something was hit?

1 Answer

1

If you weren’t using triggers and were receiving OnCollisionEnter calls, you’d get a Collision object and you could use Collision.relativeVelocity. With the relativeVelocity vector, you’d be able to determine the direction the colliding object was moving.

You'd also get a ContactPoint array, which you could use the normal of the contacts to figure the direction if you preferred.