• 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 aaronrauth95 · Nov 30, 2020 at 03:07 PM · unity 2d

The "TotalJumps" is not working at all. I tried even with 0 number of jumps in unity and still jumping like same before. How do i fix that? Appreciate any helps.

public class MainMechanics : MonoBehaviour { Rigidbody2D rb; public float movespeed = 2; float horizontalvalue;

 //Run

[SerializeField] bool isRunning = false; float runSpeedModifier = 2f;

 Animator anim;

 bool facingRight = true;

 //GroundCheck
 [SerializeField] Transform groundCheckCollider;
 [SerializeField] Transform overheadCheckCollider;
 [SerializeField] LayerMask groundLayer;
 [SerializeField] bool isGrounded = false;
 const float groundCheckRadius = 0.2f;
 const float overheadCheckRadius = 0.2f;

 //Jump
 [SerializeField] float JumpPower;
 [SerializeField] int TotalJumps;
 int AvailableJumps;
 bool MultipleJumps;
 

 //Crouching
 [SerializeField] bool isCrouching = false;
 [SerializeField] Collider2D standingCollider;
 float crouchSpeedModifier = 0.5f;

 void Start()
 {
     AvailableJumps = TotalJumps;
     rb = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
 }

 
 void Update()
 {

     //store the horizontal value
     horizontalvalue = Input.GetAxisRaw("Horizontal");
     
     //Run
     if (Input.GetKeyDown(KeyCode.LeftShift))
         isRunning = true;
     if (Input.GetKeyUp(KeyCode.LeftShift))
         isRunning = false;

     //Jump
     if (Input.GetButtonDown("Jump"))
     {
         Jumps();
     }

     //Crouching
     if (Input.GetKeyDown(KeyCode.DownArrow))
     {
         isCrouching = true;
     }
     else if (Input.GetKeyUp(KeyCode.DownArrow))
     {
         isCrouching = false;
     }

     /*//Y velocity for the jump animation from animator
     if (!isGrounded)
         anim.SetFloat("yVelocity", rb.velocity.y);
     else
         anim.SetFloat("yVelocity", 0);
 */
 }
 
 void FixedUpdate()
 {
     GroundCheck();
     movement(horizontalvalue, isCrouching);
 }

 void GroundCheck()
 {
     #region
     bool wasGrounded = isGrounded;
     isGrounded = false;
     // Check if the GroundCheckObject is colliding
     //with other colliders that are in the "Groud" Layer
     //if yes (isGrounded true) else (isGrounded false)
     Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckCollider.position, groundCheckRadius, groundLayer);
     if (colliders.Length > 0)
     {
         isGrounded = true;
         if (!wasGrounded)
         {
             AvailableJumps = TotalJumps;
             MultipleJumps = true;
         }              
     }

     anim.SetBool("jump", !isGrounded);
     #endregion
 }

 //Jump
 void Jumps()
 {
     if (isGrounded)
     {
         MultipleJumps = true;
         AvailableJumps--;

         rb.velocity = Vector2.up * JumpPower;
         anim.SetBool("jump", true);
     }
     else if (MultipleJumps && (AvailableJumps>0))
     {

         AvailableJumps--;

         rb.velocity = Vector2.up * JumpPower;
         anim.SetBool("jump", true);
     }
 }

},public class MainMechanics : MonoBehaviour { Rigidbody2D rb; public float movespeed = 2; float horizontalvalue;

 //Run

[SerializeField] bool isRunning = false; float runSpeedModifier = 2f;

 Animator anim;

 bool facingRight = true;

 //GroundCheck
 [SerializeField] Transform groundCheckCollider;
 [SerializeField] Transform overheadCheckCollider;
 [SerializeField] LayerMask groundLayer;
 [SerializeField] bool isGrounded = false;
 const float groundCheckRadius = 0.2f;
 const float overheadCheckRadius = 0.2f;

 //Jump
 [SerializeField] float JumpPower;
 [SerializeField] int TotalJumps;
 int AvailableJumps;
 bool MultipleJumps;
 

 //Crouching
 [SerializeField] bool isCrouching = false;
 [SerializeField] Collider2D standingCollider;
 float crouchSpeedModifier = 0.5f;

 void Start()
 {
     AvailableJumps = TotalJumps;
     rb = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
 }

 
 void Update()
 {

     //store the horizontal value
     horizontalvalue = Input.GetAxisRaw("Horizontal");
     
     //Run
     if (Input.GetKeyDown(KeyCode.LeftShift))
         isRunning = true;
     if (Input.GetKeyUp(KeyCode.LeftShift))
         isRunning = false;

     //Jump
     if (Input.GetButtonDown("Jump"))
     {
         Jumps();
     }

     //Crouching
     if (Input.GetKeyDown(KeyCode.DownArrow))
     {
         isCrouching = true;
     }
     else if (Input.GetKeyUp(KeyCode.DownArrow))
     {
         isCrouching = false;
     }

     /*//Y velocity for the jump animation from animator
     if (!isGrounded)
         anim.SetFloat("yVelocity", rb.velocity.y);
     else
         anim.SetFloat("yVelocity", 0);
 */
 }
 
 void FixedUpdate()
 {
     GroundCheck();
     movement(horizontalvalue, isCrouching);
 }

 void GroundCheck()
 {
     #region
     bool wasGrounded = isGrounded;
     isGrounded = false;
     // Check if the GroundCheckObject is colliding
     //with other colliders that are in the "Groud" Layer
     //if yes (isGrounded true) else (isGrounded false)
     Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckCollider.position, groundCheckRadius, groundLayer);
     if (colliders.Length > 0)
     {
         isGrounded = true;
         if (!wasGrounded)
         {
             AvailableJumps = TotalJumps;
             MultipleJumps = true;
         }              
     }

     anim.SetBool("jump", !isGrounded);
     #endregion
 }

 //Jump
 void Jumps()
 {
     if (isGrounded)
     {
         MultipleJumps = true;
         AvailableJumps--;

         rb.velocity = Vector2.up * JumpPower;
         anim.SetBool("jump", true);
     }
     else if (MultipleJumps && (AvailableJumps>0))
     {

         AvailableJumps--;

         rb.velocity = Vector2.up * JumpPower;
         anim.SetBool("jump", true);
     }
 }

}

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 aaronrauth95 · Nov 27, 2020 at 01:31 PM 0
Share

I still have some code leftover but thats the part for movement, like crouching, walking, running and so on. But i have a major problem with the multiple jumps. No matter what value i assigned to the "[SerializeField] int TotalJumps;" has no any affects to it. I would really be thankfull for any code or any helps.

avatar image swanne · Nov 30, 2020 at 05:01 PM 0
Share

Hey. This is a tricky one. I can't see anything obviously incorrect. Add a Debug.Log("Available jumps =" + AvailableJumps); on lines 105 (in the if isGrounded check) and 112. See if the number of available jumps is being interacted with in any way

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

145 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

Related Questions

Finding relative mouse position 2 Answers

Do I need to destroy objects I'm not using? 4 Answers

Seeing different amounts of the game in different resolutions 0 Answers

how to add 2d cutscenes to unity 2 Answers

Possible to change size of cursor with Cursor.SetCursor? 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