- Home /
Detect Collision From Bottom?
Hello UA,
For my game, I have an object with a box collider. When the object is grounded, the player can make the object jump. The thing is though that my script is using
function OnCollisionStay ()
{
grounded = true;
}
This is good and all but if I collide up against a wall and hold the Jump button, the object rises. What I need to do is for grounded to become true ONLY if there is collision from the bottom of the collider. How would I go about doing this? I could use raycasting but I'm not sure if that's the best choice...
Thanks In Advance :)
Answer by kromenak · Jun 14, 2011 at 09:44 PM
This question is a bit old, but hopefully this will be helpful to someone.
You can do a raycast, but we use this following method. The only problem with it is if for some reason the initial collision is not below - but you could probably use OnCollisionStay to solve that.
void OnCollisionEnter(Collision collision)
{
if(collision.contacts.Length > 0)
{
ContactPoint contact = collision.contacts[0];
if(Vector3.Dot(contact.normal, Vector3.up) > 0.5)
{
//collision was from below
}
}
}