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 thenephalem · Dec 25, 2017 at 08:56 AM · unity 5collisionjumpingground detection

Raycasting bug when checking if player is grounded

Greetings. I have the following problem. I am casting a ray to check if the player is grounded and it works properly. When I press space I jump and set isJumping flag to true and when I land to false. The problem is that sometimes when I jump the isJumping flag is set to false because the raycast still gives that the player is grounded. It is some kind of a race condition maybe since it happens 1 out of 10 times but is really annoying. My ray has a fixed length and I have set the layermask as well.

Comment
Add comment
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

2 Replies

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

Answer by theterrificjd · Dec 25, 2017 at 09:05 AM

My recommendation is to move the raycast into its own function that returns a bool. Call this everytime you need to.

 public bool IsGrounded(){
 //layermask stuff
 return Physics.Raycast(......);
 }

This way, you can add the if(IsGrounded()) before you set your isJumping to true. Next, OnCollisionEnter, add something like:

 if(IsGrounded(){
 isJumping = false;
 }

To ensure that your raycast is reset consistently when grounded.

My long term recommendation is to actually get rid of the isJumping variable entirely if you can, unless you need it for something else.

Comment
Add comment · 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
0

Answer by thenephalem · Dec 25, 2017 at 09:39 AM

But how can I set the landing animation if I do not check whether the player is grounded every few frames? In my game the player can run on walls and make huge jumps. I need to detect whether a player is jumping or has simply fallen of a wall. Maybe i should try to use like you suggested to surround each walkable object with a trigger and to check if the player is grounded only after they leave the trigger zone.

Comment
Add comment · Show 2 · 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 theterrificjd · Dec 25, 2017 at 04:31 PM 1
Share

$$anonymous$$y thinking was that if the player collides with anything (OnCollisionEnter), it will check if IsGrounded(), and then set isJumping to false.

Then you can play a game of bools, as I call it:

 // Add this up where you declared global variables (isJumping)
 private bool previouslyJumping;
 
 // Add this somewhere in an Update/Coroutine somewhere
 bool hasChanged = false;
 if (previouslyJumping != isJumping) {
     hasChanged = true;
 }
 if (hasChanged && isJumping) {
    // Just jumped this frame
 } else if (hasChanged && !isJumping) {
     // Just landed this frame
 }
avatar image theterrificjd theterrificjd · Dec 25, 2017 at 04:56 PM 1
Share

$$anonymous$$issed the piece about running on walls.

You can accomplish this with multiple versions of the same IsGrounded(), but IsWalled() or something like that, returning an integer (I usually do -1, 0, 1, 2) or something, depending on whether a raycast is hitting neither (0), right (1), left (-1), or both (2). I choose those numbers for Axis purposes (ex: resulting in Vector3.right * 1 OR -1). Perfect in combination with a switch statement.


Another thing I like to do is, if you are using colliders, ins$$anonymous$$d of using the Tag system (which is restrictive because it only allows you to have one), you can just add a script the the wall or collider named 'Climbable' or whatever, set any unique parameters you may dream of, and then add something like:

 OnCollisionEnter(Collider other){
     climbable = GetComponent<Climbable>();
     if(climbable){
           //Your player is touching a climbable wall.
          // other.normal = the direction the 'face' is facing
          //applying a small force in the opposite direction of normals clings to walls pretty nicely. especially if you apply the gravity on your own.
     }
 }

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

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

How to make two objects mimic each other? 1 Answer

How to detect collision on only one side of an object? [C#] 4 Answers

How do i prevent the player from dragging a ui element off screen? 1 Answer

Sphere (player) flattens/deforms when going on a moving platform. 0 Answers

Delay Jumping 3 Answers


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