Search Unity

Non-Stop Jumping

Discussion in 'Physics' started by Ryanath, Aug 12, 2019.

  1. Ryanath

    Ryanath

    Joined:
    Jun 19, 2018
    Posts:
    1
    Got this code from a Brackeys Tutorial Video and for some reason my character is infinitely jumping. Help would be appreciated!



    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3.  
    4. public class CharacterController2D : MonoBehaviour
    5. {
    6.     [SerializeField] private float m_JumpForce = 400f;                          // Amount of force added when the player jumps.
    7.     [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f;          // Amount of maxSpeed applied to crouching movement. 1 = 100%
    8.     [Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f;  // How much to smooth out the movement
    9.     [SerializeField] private bool m_AirControl = false;                         // Whether or not a player can steer while jumping;
    10.     [SerializeField] private LayerMask m_WhatIsGround;                          // A mask determining what is ground to the character
    11.     [SerializeField] private Transform m_GroundCheck;                           // A position marking where to check if the player is grounded.
    12.     [SerializeField] private Transform m_CeilingCheck;                          // A position marking where to check for ceilings
    13.     [SerializeField] private Collider2D m_CrouchDisableCollider;                // A collider that will be disabled when crouching
    14.  
    15.     const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
    16.     private bool m_Grounded;            // Whether or not the player is grounded.
    17.     const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up
    18.     private Rigidbody2D m_Rigidbody2D;
    19.     private bool m_FacingRight = true;  // For determining which way the player is currently facing.
    20.     private Vector3 m_Velocity = Vector3.zero;
    21.  
    22.     [Header("Events")]
    23.     [Space]
    24.  
    25.     public UnityEvent OnLandEvent;
    26.  
    27.     [System.Serializable]
    28.     public class BoolEvent : UnityEvent<bool> { }
    29.  
    30.     public BoolEvent OnCrouchEvent;
    31.     private bool m_wasCrouching = false;
    32.  
    33.     private void Awake()
    34.     {
    35.         m_Rigidbody2D = GetComponent<Rigidbody2D>();
    36.  
    37.         if (OnLandEvent == null)
    38.             OnLandEvent = new UnityEvent();
    39.  
    40.         if (OnCrouchEvent == null)
    41.             OnCrouchEvent = new BoolEvent();
    42.     }
    43.  
    44.     private void FixedUpdate()
    45.     {
    46.         bool wasGrounded = m_Grounded;
    47.         m_Grounded = false;
    48.  
    49.         // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
    50.         // This can be done using layers instead but Sample Assets will not overwrite your project settings.
    51.         Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
    52.         for (int i = 0; i < colliders.Length; i++)
    53.         {
    54.             if (colliders[i].gameObject != gameObject)
    55.             {
    56.                 m_Grounded = true;
    57.                 if (!wasGrounded)
    58.                     OnLandEvent.Invoke();
    59.             }
    60.         }
    61.     }
    62.  
    63.  
    64.     public void Move(float move, bool crouch, bool jump)
    65.     {
    66.         // If crouching, check to see if the character can stand up
    67.         if (!crouch)
    68.         {
    69.             // If the character has a ceiling preventing them from standing up, keep them crouching
    70.             if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
    71.             {
    72.                 crouch = true;
    73.             }
    74.         }
    75.  
    76.         //only control the player if grounded or airControl is turned on
    77.         if (m_Grounded || m_AirControl)
    78.         {
    79.  
    80.             // If crouching
    81.             if (crouch)
    82.             {
    83.                 if (!m_wasCrouching)
    84.                 {
    85.                     m_wasCrouching = true;
    86.                     OnCrouchEvent.Invoke(true);
    87.                 }
    88.  
    89.                 // Reduce the speed by the crouchSpeed multiplier
    90.                 move *= m_CrouchSpeed;
    91.  
    92.                 // Disable one of the colliders when crouching
    93.                 if (m_CrouchDisableCollider != null)
    94.                     m_CrouchDisableCollider.enabled = false;
    95.             }
    96.             else
    97.             {
    98.                 // Enable the collider when not crouching
    99.                 if (m_CrouchDisableCollider != null)
    100.                     m_CrouchDisableCollider.enabled = true;
    101.  
    102.                 if (m_wasCrouching)
    103.                 {
    104.                     m_wasCrouching = false;
    105.                     OnCrouchEvent.Invoke(false);
    106.                 }
    107.             }
    108.  
    109.             // Move the character by finding the target velocity
    110.             Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
    111.             // And then smoothing it out and applying it to the character
    112.             m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
    113.  
    114.             // If the input is moving the player right and the player is facing left...
    115.             if (move > 0 && !m_FacingRight)
    116.             {
    117.                 // ... flip the player.
    118.                 Flip();
    119.             }
    120.             // Otherwise if the input is moving the player left and the player is facing right...
    121.             else if (move < 0 && m_FacingRight)
    122.             {
    123.                 // ... flip the player.
    124.                 Flip();
    125.             }
    126.         }
    127.         // If the player should jump...
    128.         if (m_Grounded && jump)
    129.         {
    130.             // Add a vertical force to the player.
    131.             m_Grounded = false;
    132.             m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
    133.         }
    134.     }
    135.  
    136.  
    137.     private void Flip()
    138.     {
    139.         // Switch the way the player is labelled as facing.
    140.         m_FacingRight = !m_FacingRight;
    141.  
    142.         // Multiply the player's x local scale by -1.
    143.         Vector3 theScale = transform.localScale;
    144.         theScale.x *= -1;
    145.         transform.localScale = theScale;
    146.     }
    147. }
    Code (CSharp):
    1.  
    2. public class PlayerMovement : MonoBehaviour
    3. {
    4.  
    5.     public CharacterController2D controller;
    6.     public float runspeed = 40f;
    7.  
    8.     bool jump = false;
    9.  
    10.     float horizontalMove = 0f;
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         horizontalMove = Input.GetAxisRaw("Horizontal") * runspeed;
    15.         if (Input.GetButtonDown("Jump"))
    16.         {
    17.             jump = true;
    18.         }
    19.     }
    20.  
    21.     private void FixedUpdate()
    22.     {
    23.         //Move Our Character
    24.         controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
    25.  
    26.     }
    27. }
    28.  
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    723
    In your PlayerMovement class:

    Code (CSharp):
    1. void Update()
    2. {
    3.     //...
    4.  
    5.     if (Input.GetButtonDown("Jump"))
    6.             {
    7.                 jump = true;
    8.             }
    9. }
    10. private void FixedUpdate()
    11.     {
    12.         //Move Our Character
    13.         controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
    14.     }
    So you are setting jump on the frame the jump button is pressed down. Each FixedUpdate you send call the movement function and send the value in jump along. There, you have:

    Code (CSharp):
    1. if (m_Grounded && jump)
    2. {
    3.     //Add a vertical force to the player.
    4.     m_Grounded = false;
    5.     m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
    6. }
    Now an upward force is added, making the object pop up. It also sets the grounding flag to false, so the force is only applied for a moment, preventing the object from shooting up to space. However, nothing sets
    jump to false, so next time the object is grounded again, it will just jump right away.

    You could add a line to set
    jump to false in the above if block. This would work, but it would also mean that jump will ONLY be cleared when the character actually jumps. The result is that pressing jump while not allowed to jump, like while the object is in the air, will still set jump to true. Now, next time the object is grounded, no matter how long it takes, it will jump once automatically.

    You will either need to reset the jump flag when the button is released, or not allow it to be set in the first place if the object is not grounded. After this, it should work. Let us know if you need help implementing this.