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 /
  • Help Room /
avatar image
0
Question by Starklndustries · Jan 28, 2020 at 09:19 PM · unity 2djumpinggroundgrounded

Cant double jump with grounded code,Being grounded and Double Jumping

I have this code where it works great for me so far, except the jumping, no matter how I try to do it I can't seem to be able to double jump I've gotten to the point where I can double jump but it is an infinite double jump because I don't know how to set the JumpCounter back to zero because if I put it in the second jumping line of code the counter never goes up or it adds up together if I change the value to anything. ,I am trying to make it so that my player double jumps and have gotten to the point where if he jumps the value changes to 1 and he can double jump, if the value is not at 1 he will not double jump, my problem is resetting the value to 0 so that the player cant double jump anymore. If I type anything that has to do with JumpCount in the second line of jumping they both add up together or if I set JumpCount =0 it just wont move up.

public class Player_Controller : MonoBehaviour { Animator animator; Rigidbody2D rb2d; SpriteRenderer spriteRenderer;

 bool isGrounded;

 [SerializeField]
 Transform groundCheck;
 
 [SerializeField]
 private float runSpeed = 2;
 
 [SerializeField]
 private float jumpSpeed = 3;

 [SerializeField]
 private int JumpCount = 1;

 //use this for initialization
 private void Start()
 {
     animator = GetComponent<Animator>();
     rb2d = GetComponent<Rigidbody2D>();
     spriteRenderer = GetComponent<SpriteRenderer>();
 }

 private void FixedUpdate()
 {
     if(Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")))
     {
         isGrounded = true;
     }
     else
     {
         isGrounded = false;
     }

     if(Input.GetKey("d") || Input.GetKey("right"))
     {
         rb2d.velocity = new Vector2(runSpeed, rb2d.velocity.y);
        
         if(isGrounded)
             animator.Play("Player_run");
         spriteRenderer.flipX = false;
     }
     else if(Input.GetKey("a") || Input.GetKey("left"))
     {
         rb2d.velocity = new Vector2(-runSpeed, rb2d.velocity.y);
         if (isGrounded)
             animator.Play("Player_run");
         
         spriteRenderer.flipX = true;
     }
     else
     {
         if (isGrounded)
             animator.Play("Player_idle");
         
         rb2d.velocity = new Vector2(0, rb2d.velocity.y);
     }

     if (isGrounded)
     {
         JumpCount = 0;
     }

     if(Input.GetKey("space") && isGrounded)
     {
         JumpCount = 1;
         rb2d.velocity = new Vector2(rb2d.velocity.x, jumpSpeed);
         animator.Play("Player_jump");
     }

     if (Input.GetKey("space") && !isGrounded && JumpCount == 1)
     {
         rb2d.velocity = new Vector2(rb2d.velocity.x, jumpSpeed);
         JumpCount = 2;
     }
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

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

Ground the object to only jump once in 2D platformer game. 0 Answers

Unity 2D Enemy jump Question (Frog jump) 1 Answer

2D game jumping stopped working when I added lighting 0 Answers

Jump Script in Unity 2D,I need help with jump script unity 2d 0 Answers

Player no longer jumping 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