- Home /
Detect if there's collision from below? (box collider)
Hi, I'm making a sidescroller controller and cant get pass the jumping. I need to know when the player is on ground. I know I can raycast but it's not working right with just one raycast, and I can't figure out working with multiple rays.
After a bit of googling and searching I got this far.
function OnCollisionStay(coll : Collision)
{
if(coll.contacts.Length > 0)
{
var contact = coll.contacts[0];
if(Vector3.Dot(contact.normal, Vector3.up) > 0.5)
{
onGround = true;
}
}
}
But as it works when on ground, I don't know how to check when its off ground. I tried a bunch different else if's but none worked.
Any suggestions? Or a better way for doing jumping?
Thank you!
Answer by Vascoptorres · Nov 24, 2013 at 07:45 PM
I suggest you to use a Character Controller.
Here look at this link: http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html
It even has the code for the jump that you want.
I know but for learning purposes I want to make my own. So no character controller, I already know it has everything I just won't learn anything that way. :S
Answer by PaulosCreations · Dec 06, 2013 at 05:48 PM
what i got from the wiki script is you put an --onGround=false-- at the bottum of you`re fixedUpdate function.
that will set grounded to false and will stay false unless you`re collision detect function sets it true.