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 05:56 AM · 2djumpgroundgrounded

Jumping when not grounded

My character seems to jump when not grounded. I can jump without being o the ground. I have created a ground check and a layer mask (so that the player can only jump off the floor).

However in my code the I have made a debug log saying "grounded" every time I am on the ground. But for some reason I can jump even when I am not grounded (no messages appear in debug log while I am continuously jumping). I should not be able to jump but I can.

The Player and the ground check are on a "Player" layer and the floor is on a "Floor" layer. whatIsGround is a public layer mask which uses only the "Floor" layer.

 void Update () 
     {
         if (Input.GetButtonDown ("Jump") && grounded) // "Jump" is the spacebar key on the keyboard
         {
             if(grounded)
                 Player.AddForce (new Vector2 (jumpSpeed, jumpForce));
                 // makes the character jump
         }

and in fixed update

 void FixedUpdate ()
     {
         grounded = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround);

 if (grounded)
             Debug.Log("grounded");


It doesn't make much sense to me my code seems perfectly fine (these are just snippets of the code that refer to grounded and the jumping problem)

Comment
Add comment · Show 1
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 Xeong-Hu · Apr 24, 2015 at 09:36 AM 0
Share

Hmm,, try taking out that extra bracket statement that says if(grounded) inside the Input if statement.

I don't think you'll need that since you'll only be adding force if you're grounded and you press the Space button.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Fappp · Apr 24, 2015 at 10:02 AM

At first glance it looks to me grounded is never set to false. Why did you decide to go with this approach?

The way I would tackle this:

  • Give everything walkable the tag Floor

  • Create a 2D boxcollider on the player thats marked as trigger.

  • If the trigger enters > grounded = true;

  • If the trigger exits > grounded = false;

I use this approach for basically any action I want to perform with collisions because it avoids the Update function in most cases. ( In this particular case you're bound to update because of Rigidbodies )

Please also note having a boxCollider as trigger doesn't interfere with your physics, raycasts or world collision, it's only for triggering purposes.

TriggerEnter for 2D : Link TriggerExit for 2D : Link

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 CAM3RON · Apr 25, 2015 at 01:16 AM 0
Share

How would I make it so that the collider only triggers when it hits the floor tag?

avatar image Fappp · Apr 25, 2015 at 08:20 AM 0
Share

Here's the JS I've used for the demo:

 #pragma strict
 
 var grounded : boolean;
 
 var rb : Rigidbody;
 
 function Start(){
     rb = gameObject.GetComponent(Rigidbody);
 }
 
 function Update(){
 
     if ( grounded ){
         Jump();
     }
 }
 
 function Jump(){
     rb.AddForce( Vector3.up * 100 );
 }
 
 
 function OnTriggerEnter ( other : Collider ) {
     if ( other.gameObject.CompareTag("Floor")){
         grounded = true;
     }
 }
 
 function OnTriggerExit ( other : Collider ) {
     grounded = false;
 }
avatar image Fappp · Apr 25, 2015 at 08:26 AM 0
Share

Conclusively you should test the tag of the object hit, as in:

if ( other.gameObject.CompareTag("Floor")){ // Perform action }

avatar image
0

Answer by Sondre-S · Apr 24, 2015 at 10:28 AM

I usually do a raycast to detect if the player is on the ground.

      // distance to ground
         var hit : RaycastHit;
      
         if (Physics.Raycast (Character.position, -Vector3.up, hit,100)) {
             distanceToGround = hit.distance;
         }
         if(distanceToGround>0.6){OnGround=false;}
         if(distanceToGround<0.6){OnGround=true;}
     
     //jump
     
     if(Input.GetAxis("Jump")&&Key && onGround && JumpTimer<0.1)
     {
     VerticalSpeed= 0.3;
     Jump=true;
     Key=false;
     }
     if(!Input.GetAxis("Jump")&&!Key){Key=true;} // check if key is already pressed
     
     if(distanceToGround>3)
     {
     VerticalSpeed= 0;
     }
     
     if(Jump){
     JumpTimer+=0.01;
     }
     
     if(JumpTimer>JumpTime)
     {
     Jump=false;
     JumpTimer=0;
     }
     
     ///// Move character
 
 Character.Translate(SidewaysSpeed,VerticalSpeed,ForwardSpee;    
 
 
 
 
 
 
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 Fappp · Apr 24, 2015 at 12:10 PM 0
Share

Raycasting is too heavy on performance. Check this: http://fb-projecten.com/stress/ Press E to switch between ray and trigger

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

How to fix jumping from Unity2D tutorial? 2 Answers

Unity ignoring bool grounded 1 Answer

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