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 BlazeCrow · Mar 14, 2018 at 01:29 AM · jumpgroundeddouble jump

problems with grounded and double jumping

I mange to work with the regular jumping but when I implemented the double jump then it started to act funny. I can only jump once and as soon I get rid of double jump it can now infinite jump. I can't seem figure hot to stop the infinite jump and how I got to work working right in the first place.

public class PlayerPlatformController : MonoBehaviour {

 public float topSpeed = 10f;
 bool faceToRight = true;
 public bool grounded = false;
 
 public Transform groundCheck;
 public LayerMask whatIsGround;
 public float groundRadius;
 public float jumpVelocity = 7;
 public bool doubleJump = false;
 

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

     float move = Input.GetAxis("Horizontal");
     
     GetComponent<Rigidbody2D>().velocity = new Vector2(move * topSpeed, GetComponent<Rigidbody2D>().velocity.y);
     
     if (move > 0 && !faceToRight)
         Flip();
     else if (move < 0 && faceToRight)
         Flip();

 }
 
     void Update() {
     if (grounded)
     {
         doubleJump = false;
     }
     

     if (Input.GetKeyDown(KeyCode.Space ) && grounded && doubleJump)
     {
         GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;


     }
     if (Input.GetKeyDown(KeyCode.Space) && !grounded && !doubleJump)
     {
         GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;

         doubleJump = true;
      

     }

 }

 void Flip()
 {
     faceToRight = !faceToRight;

     Vector3 theScale = transform.localScale;

     theScale.x *= -1;

     transform.localScale = theScale;
     
 }
 

}

Comment
Add comment · Show 6
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 RedHedZed · Mar 14, 2018 at 01:30 PM 0
Share

Hello! Can I ask about your decision to use Physics2D.OverlapCircle for checking if the player is grounded? What object does groundCheck refer to?

avatar image BlazeCrow RedHedZed · Mar 14, 2018 at 03:16 PM 0
Share

I was using a tutorial on jumping and didn't pay much to it. I thought it had something do with the layer mask I made called ground in the tutorial. Can you give more detail what it does because I've been stuck at this for a couple hours?

avatar image RedHedZed BlazeCrow · Mar 14, 2018 at 03:29 PM 0
Share

It's not necessarily wrong. I just don't know what your game is like, so maybe it's the best choice. Physics2d.OverlapCircle just checks to see what colliders fall within a circle. The circle is located at groundCheck.position in your code. I was just curious what object that refers to, is all.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by hrishihawk · Mar 15, 2018 at 05:58 AM

@BlazeCrow

Your first problem is that your boolean variable grounded is never set to true.Which means your ground check condition fails for some reason. It's probably happening because your groundRadius value is too small. So try changing your groundRadius variable and check if grounded has the expected value. Your first step is to make sure grounded variable actually set to true when the character is on the ground before moving on to implement jump and double jump.

Even if you get the ground checking to work right your script won't work as expected because you have got your conditions for jumping and double jumping wrong.


First let's solve jumping : You want your character to jump when the following conditions are met

  1. If the character is on the ground ( when grounded = true )

  2. If the user presses Space key ( when Input.GetKeyDown(KeyCode.Space) )


Now let's do the same for double jump : Your character can jump again if the following conditions are met :

  1. If the character is in air ( when grounded = false )

  2. If the user presses Space key ( when Input.GetKeyDown(KeyCode.Space) )

  3. If`doubleJump = true`


Now using conditions for both jumping and double jumping your script becomes :

     if (Input.GetKeyDown(KeyCode.Space ) && !grounded && doubleJump)
         {
             GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;
         }
     if (Input.GetKeyDown(KeyCode.Space) && grounded)
         {
             GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;
             doubleJump = true;
         }

If you still got any doubts feel free to comment.

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 BlazeCrow · Mar 16, 2018 at 01:17 AM 0
Share

thank you for the advice it fix the infinite jumping but I can't mange to make character double jump.

avatar image hrishihawk · Mar 16, 2018 at 04:36 AM 0
Share

Ok I looked through your script, found an issue.Get rid of this :

      if (grounded)
      {
          doubleJump = false;
      }

and add doubleJump = false here :

      if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space ) && !grounded && doubleJump)
          {
              GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;
              doubleJump = false;
          }


avatar image hrishihawk · Mar 16, 2018 at 04:40 AM 0
Share

@BlazeCrow and it's better do all the physics in FIxedUpdate.

avatar image
0

Answer by Harinezumi · Mar 14, 2018 at 03:44 PM

Maybe the problem is that FixedUpdate() is called less frequently (every 0.02 seconds) than Update() (every frame), and you set grounded in FixedUpdate(), but use it to set doubleJump to false in Update().

Try moving the line grounded = Physics2D.OverlapCircle(...); to the beginning of Update(), and see if that helps (I know that it is part of physics and probably uses distances, but it's just a circle overlap, it won't have that much impact on performance).

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 BlazeCrow · Mar 15, 2018 at 12:22 AM 0
Share

nah it didn't work but as I as I jump the doubleJump is checked and I can't jump after that.

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

76 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

Related Questions

About character doublejumping. 1 Answer

Double Jump using First Person Controller 1 Answer

Jumps never reset 2 Answers

Jump of 2d character is sometimes stronger. 1 Answer

Double jump does not work why? 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