Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by S_Byrnes · Nov 17, 2015 at 11:31 PM · c#jumpsphereoncollisionstay

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image instruct9r · Nov 17, 2015 at 11:50 PM 0
Share

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...

avatar image S_Byrnes instruct9r · Nov 17, 2015 at 11:57 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image S_Byrnes · Nov 19, 2015 at 01:06 AM 0
Share

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?

avatar image glhfGreg S_Byrnes · Nov 19, 2015 at 07:01 AM 0
Share

Just make normalDirection a member function just like canJump and write the normal to that variable on every execution of OnCollisionStay.

avatar image S_Byrnes glhfGreg · Nov 19, 2015 at 07:27 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges