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 HNKMaster · Oct 16, 2015 at 12:02 AM · charactercontrollerjumpground detection

Character controller check ground while falling.

Hi everybody!

I'm having problems while working with CharacterController at making it jumping in my platform game. I made it so, when you press the Jump button, it set the "onAir" bool to TRUE, and set the vertical speed needed to make the controller jump. So, while the character is on air and falling, I want it to check if is touching the ground while is on air, making the "onAir" bool FALSE. Unfortunately, "isGrounded" doesn't work for checking if the controller touch the ground while falling, so the "onAir" stays always TRUE, and the controller can't jump again. I don't want to set the "onAir = false" while "onGrounded", because sometimes, the controller stays on ground (replacing "GetButtonDown" with "GetButton" evades this problem, but it works like doing the jump TWO/THREE times). The thing I need, is a method that makes the controller check if is touching the ground while falling.

Here is my code:

         if ( controller.isGrounded ){
 
             if (vertSpeed <= -5)
                 vertSpeed = -5;
 
             if ( Input.GetButtonDown("Jump") && !onAir ){
                 Debug.Log ("IS JUMP");
                 jmpTime = 0.2f;
                 onAir = true;
                 vertSpeed = jumpStrength;
             }
         }
         
         if ( !controller.isGrounded ){
 
             jmpTime -= Time.deltaTime;
 
             if ( controller.isGrounded && vertSpeed < 0f && jmpTime <= 0f){
                 onAir = false;
             }
         }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GiyomuGames · Oct 16, 2015 at 01:05 AM

My first comment is that onAir and isGrounded should not be true at the same time. Setting onAir to false when isGrounded is true is the right thing to do. If onAir is supposed to remain true when your controller is on the ground to prevent it to jump again then either you need to rename the variable ("hasJumpedOnce"?) or you need another logic to prevent your character from jumping again.

Now, if you want to stick to this, you can do a raycast towards the ground to check if your controller is close to / on the ground. That is basically what your controller is doing to update "isGrounded" though.

If I am missing something please explain because I am a bit confused by what you are trying to achieve here.

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 HNKMaster · Oct 16, 2015 at 01:35 AM 0
Share

Actually, I resolved the issue with another method, but I'm gonna explain that later.

$$anonymous$$aybe I redacted the text bad, but I didn't wanted setting "onAir = false" on every instance of "isGrounded", what I wanted is set "onAir = false" at the moment the character touches the floor.

What I did was, add a new bool "chkGround". On the "!isGrounded" section, while vertical speed is below 0, it sets "chkGround = true":

             if ( !controller.isGrounded ){
     
                 if (vertSpeed < 0)
             chkGround = true;
             }

So, when the controller touches the ground, I put a condition inside the "isGrounded" section which checks if "chkGround" is true:

             if ( controller.isGrounded ){
     
                 if (vertSpeed <= -5)
                     vertSpeed = -5;
                 
                 //This check the ground once.
                 if (chkGround){
                 onAir = false;
                 chkGround = false;
             }
 
                 if ( Input.GetButtonDown("Jump") && !onAir ){
                     Debug.Log ("IS JU$$anonymous$$P");
                     onAir = true;
                     vertSpeed = jumpStrength;
                 }
             }

With that, the "onAir" variable sets false at once, because the "chkGround" stops working. This is useful for situations like wanting the player stops doing aereal attacks at the moment it touches ground.

Anyways, thanks for the input.

avatar image GiyomuGames HNKMaster · Oct 16, 2015 at 01:37 AM 0
Share

No problem, the important is that you got your problem sorted out :)

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

35 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

Related Questions

Network Client does not Groundcheck 0 Answers

Jumping not always work 2 Answers

I aren't able to jump. I find no mistake in the script 1 Answer

How to jump multiple times to mid air? 3 Answers

I cant make my Character jump. 0 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