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 CAM3RON · Apr 24, 2015 at 04:29 PM · 2djumpgroundgrounded

Unity ignoring bool grounded

My character jumps when I press the spacebar. My code says that it can only jump if it is grounded however it is still jumping when not grounded.

I have set up a debug log for grounded and not grounded. So i can see that i am jumping while the debug log is saying that I am not grounded.

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 hbalint1 · Apr 24, 2015 at 05:23 PM 0
Share

you should post your code, so we could help.

avatar image CAM3RON · Apr 25, 2015 at 01:13 AM 0
Share

I found out the error. I have two other checks for each side of my character and the players collider is hitting both making my character jump when not grounded.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ethan-smith · Apr 26, 2015 at 09:35 AM

 private float collidercentertoedge;
 public float groundmargin = 0.05f;
 private RaycastHit ground;
 private bool Isgrounded;
 
 void Start(){
    CapsuleCollider collider = GetComponenet<CapsuleCollider>();
    collidercentertoedge = collider.bounds.extents.y - collider.center.y;
 }
 
 void Update(){
    Rigidbody body = GetComponenet<Rigidbody>();
    if(Physics.Raycast(transform.position, -transform.up, out ground)){
       Isgrounded = ground.distance <= collidercentertoedge + groundmargin;
 
       if(Input.GetKeydown(KeyCode.Space) & Isgrounded){
 
          JUMP CODE HERE
          JUMP CODE HERE
 
          OR 
 
          body.velocity.y *= jumpforce; 
 
       }
 
 
    }else {
       Isgrounded = false;
    }
 }




it checks using raycast to see if there is something beneath you. if there is then we get the distance from the center of the collider to the bottom of it and then add the ground margin (the margin is incase you fall into a pit and you aren't touching the ground but the sides of it are holding you up, like if you fell down a narrow cone. if this happens and you still cant touch then increase the margin variable) and checks to see if the distance from the cast position to the hit position is less than or equal to this. if it is then you are touching ground or are withing the variable "groudmargin" from the ground. if there is nothingbeneath you then the raycast = false and so does "isgrounded"

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Jumping when not grounded 2 Answers

How to fix jumping from Unity2D tutorial? 2 Answers

Jump of 2d character is sometimes stronger. 1 Answer

2d grounded check problem? 3 Answers

Unity 2D Platformer: How to properly implement this ground check? 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