Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Gurunext · Aug 04, 2013 at 06:02 PM · collisionjumpingfloor

Solved: How to know if an object collides with anything at all? (Jumping issue)

This is a pretty simple jumping script,

     void onUpdate() {
             if (Input.GetButtonDown("Jump") && onGround)
                     rigidbody.velocity += new Vector3(0, jumpVelocity/50, 0);
     }
     
     void OnCollisionStay(Collision collision) {
         foreach (ContactPoint contact in collision.contacts)
         if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
             onGround = true;
     }
         
     void OnCollisionExit (Collision collision) {
         onGround = false;
     }

but you see the problem is that when the object (let's say floor) gets destroyed and there are no other colliders, OnCollisionExit is not triggered and OnCollisionStay also stops functioning. In which case my player can still jump infinitely many times until I hit a floor.

I wonder if there is maybe a way to know if a player does or does not collide with anything at all. That would solve my problem.

Comment
Add comment · Show 7
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 Hyperion · Aug 04, 2013 at 06:34 PM 0
Share

You'd do that simply by saying

 void OnCollisionEnter(Collision collision){
 //check whatever boolean variable true
 }

This is checking your hit or whatever variable true upon any collision.

avatar image Gurunext · Aug 05, 2013 at 02:56 PM 0
Share

I thought of a better way which let me know if OnCollisionStay() happens or not, in other words I would know if any collision is happening and check if collision is correct for me. But I forgot to mention couple very important lines

 if (onGround)
     rigidbody.useGravity = false;
     else rigidbody.useGravity = true;

This gravity adjusting code (if player is on the ground or not) makes stuff very glitchy especially when ground actually disappears. It makes sense to always apply gravity, even when object is standing on something solid (I just followed ETesskiTutorials)

avatar image Hyperion · Aug 05, 2013 at 04:34 PM 0
Share

I thought what you wanted was to do was prohibit movement when not on ground, but that code seems to do the opposite with gravity.

avatar image Hyperion · Aug 05, 2013 at 04:35 PM 0
Share

I see it. So you only want to adjust gravity? I thought gravity works that way on its own... Alright then.

avatar image Gurunext · Aug 05, 2013 at 05:59 PM 0
Share

Yes, but it doesn't work. Player just keep bumping up and down (slightly, but pretty irritating) It seems these lines of code just messed everything up. Because when gravity is off and collider is gone, it doesn't detect that it lost the collider, so onGround was left true and gravity left false, what made me float in the air and jump infinitely high. That seems to be the problem :P

(if it's questionable everything else works with the code, player jumps once and so on. In air gravity was activated since player actually left the collider by himself)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Hyperion · Aug 04, 2013 at 06:45 PM

But here's my code to check if you're on the ground or not, and it works:

 void OnCollisionEnter(Collision hit){
     if (hit.gameObject.tag=="terrain"){
         grounded=true;
     }
 }
 void OnCollisionExit(Collision hit){
     if (hit.gameObject.tag=="terrain"){
         grounded=false;
     }
 }

Hope this helped - Hyperion

Comment
Add comment · Show 1 · 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 Ziplock9000 · Dec 23, 2020 at 03:58 PM 0
Share

This solution is for "when" via an event, not "if" via a test which is what the title asks.

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

16 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

Related Questions

Delay Jumping 3 Answers

Box collider didn't jump with player 1 Answer

SweepTest to check if grounded 1 Answer

My jump code seems to be broken, the character's flags for jumping appear to be working strangely. 1 Answer

Detecting Collision for Jumping 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