- Home /
OnCollision Jump Occurring Twice
Hi guys, I have a problem with my spherical player jumping more times than it should if it's near two ground colliders, I've been able to fix this problem before using rays, but I've recently changed it so I get the normals of the object collision, there-for I need OnCollision for this to work correctly.
So I need to be able to only jump once even if touching two colliders.
void OnCollisionStay(Collision theCollision)
{
if (theCollision.gameObject.tag == "Ground")
{
onGround = true;
canJump = true;
Vector3 normalDirection = theCollision.contacts[0].normal;
if (Input.GetButton("Jump") && canJump)
{
GetComponent<Rigidbody>().AddForce(normalDirection * jumpDistY);
canJump = false;
}
}
}
void OnCollisionExit(Collision theCollision)
{
if (theCollision.gameObject.tag == "Ground")
{
onGround = false;
}
}
If you might know the answer to this, please let me know, I'd very much appreciate the help! Thank you!
1: What's the point of onGround?
2: $$anonymous$$ove canJump above the AddForce. When you want something to be disabled, right after it's activated put the Disable first and then below, put the rest of the code. This way if the code execition needs more time, you won't risk calling it again, before it reaches canJump...
Thanks for the reply!
With 1, it's for my movement code up above the OnCollisions.
2, that's a great tip, thanks, but it still didn't solve the problem, because it's possible for my player to be touching both colliders simultaneously.
Any way to relieve this issue to your knowledge?
Answer by glhfGreg · Nov 18, 2015 at 10:32 AM
From Unity docs:
OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.
If your player is colliding with two colliders, the OnCollisionStay function will be called twice. On each of these calls you set canJump to true, then use it in a condition. The result is that canJump will always be true when it's checked in the jump condition.
The solution is to call AddForce in the FixedUpdate (or Update) function. Use OnCollisionStay only to set canJump to true and OnCollisionExit to set it to false. Check canJump in the Update function and AddForce will fire only once.
Thank you very much, I have tried this originally though, but the problem I had the first time was that I need the theCollision.contact[0].normals to work, and I use the variable 'normalDirection' as the direction multiply the jump vector by but it doesn't recognize it up in the FixedUpdate area if it's been set in the OnCollisionStay function.
Do you know how I might have my cake and eat it too?
Just make normalDirection a member function just like canJump and write the normal to that variable on every execution of OnCollisionStay.
Ahhh, thank you very much!
At first I didn't understand what you meant, since I tried something similar to this before, but I realize now that I had a Vector3 in front of each normalDirection inside of the OnCollisionStay.
Thanks again, I really appreciate the help! - I've accepted, up-voted and rewarded you some of my points.
Your answer
Follow this Question
Related Questions
Sphere can jump, but doesn't stop. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Marble refuses to jump sometimes 3 Answers
Jump with Character Controller 1 Answer