Search Unity

Holding jump key player goes up forever.

Discussion in 'Physics' started by Deleted User, Jun 27, 2020.

  1. Deleted User

    Deleted User

    Guest

    I'm trying to add a double jump but I'm having issues. The double jump works but when I hold the jump key you go upwards forever. I dont understand coding can someone please help me. Im very new.





    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6.  
    7. public class RainbowObserver : MonoBehaviour
    8. {
    9.     /// <summary>
    10.     float dirX;
    11.  
    12.     [SerializeField]
    13.     float jumpForce = 400f;
    14.  
    15.  
    16.  
    17.     bool doubleJumpAllowed = false;
    18.     bool onTheGround = false;
    19.  
    20.     public int curHealth;
    21.     public int maxHealth = 5;
    22.  
    23.     Animator animator;
    24.     Rigidbody2D rb2d;
    25.     SpriteRenderer spriteRenderer;
    26.     private Collider2D coll;
    27.     private enum State { idle, running, jumping, hurt }
    28.     private State state = State.idle;
    29.  
    30.     bool isGrounded;
    31.  
    32.     [SerializeField]
    33.     Transform groundCheck;
    34.  
    35.     [SerializeField]
    36.     Transform groundCheckL;
    37.  
    38.     [SerializeField]
    39.     Transform groundCheckR;
    40.  
    41.     [SerializeField]
    42.     private float runspeed = 11f;
    43.  
    44.  
    45.     [SerializeField] private float hurtForce = 10f;
    46.  
    47.  
    48.  
    49.  
    50.     private void OnCollisionEnter2D(Collision2D other)
    51.     {
    52.         if (other.gameObject.tag == "Enemy")
    53.  
    54.         {
    55.             if (state == State.jumping)
    56.             {
    57.                 Destroy(other.gameObject);
    58.                 Jump();
    59.  
    60.             }
    61.             else
    62.             {
    63.                 if (other.gameObject.transform.position.x > transform.position.x)
    64.                 {
    65.                     rb2d.velocity = new Vector2(-hurtForce, rb2d.velocity.y);
    66.                 }
    67.                 else
    68.                 {
    69.                     rb2d.velocity = new Vector2(hurtForce, rb2d.velocity.y);
    70.                 }
    71.             }
    72.         }
    73.  
    74.     }
    75.  
    76.     void Start()
    77.     {
    78.         curHealth = maxHealth;
    79.  
    80.  
    81.         animator = GetComponent<Animator>();
    82.         rb2d = GetComponent<Rigidbody2D>();
    83.         spriteRenderer = GetComponent<SpriteRenderer>();
    84.  
    85.  
    86.     }
    87.  
    88.  
    89.     private void Update()
    90.     {
    91.         if ((Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"))) ||
    92.           (Physics2D.Linecast(transform.position, groundCheckL.position, 1 << LayerMask.NameToLayer("Ground"))) ||
    93.           (Physics2D.Linecast(transform.position, groundCheckR.position, 1 << LayerMask.NameToLayer("Ground"))))
    94.         {
    95.             isGrounded = true;
    96.         }
    97.         else
    98.         {
    99.             isGrounded = false;
    100.             animator.Play("Observer_rainbow_jump");
    101.         }
    102.  
    103.         if (Input.GetKey("d"))
    104.         {
    105.             rb2d.velocity = new Vector2(runspeed, rb2d.velocity.y);
    106.  
    107.             if (isGrounded)
    108.                 animator.Play("Observer_rainbow_run");
    109.  
    110.             spriteRenderer.flipX = false;
    111.         }
    112.         else if (Input.GetKey("a"))
    113.         {
    114.             rb2d.velocity = new Vector2(-runspeed, rb2d.velocity.y);
    115.  
    116.             if (isGrounded)
    117.                 animator.Play("Observer_rainbow_run");
    118.             spriteRenderer.flipX = true;
    119.         }
    120.         else
    121.         {
    122.             if (isGrounded)
    123.                 animator.Play("Observer_rainbow_idle");
    124.  
    125.             rb2d.velocity = new Vector2(0, rb2d.velocity.y);
    126.         }
    127.  
    128.    
    129.  
    130.         VelocityState();
    131.         animator.SetInteger("state", (int)state);
    132.  
    133.  
    134.         /// </summary>
    135.         if (rb2d.velocity.y == 0)
    136.             onTheGround = true;
    137.         else
    138.             onTheGround = false;
    139.  
    140.         if (onTheGround)
    141.             doubleJumpAllowed = true;
    142.  
    143.         if (onTheGround && Input.GetKey("w"))
    144.         {
    145.             Jump();
    146.         }
    147.         else if (doubleJumpAllowed && Input.GetKey("w"))
    148.         {
    149.             Jump();
    150.             doubleJumpAllowed = false;
    151.         }
    152.  
    153.         dirX = Input.GetAxis("Horizontal") * runspeed;
    154.         /// </summary>
    155.  
    156.  
    157.         if (curHealth > maxHealth)
    158.         {
    159.             curHealth = maxHealth;
    160.         }
    161.  
    162.         if (curHealth <= 0)
    163.         {
    164.             Die();
    165.         }
    166.     }
    167.  
    168.     /// </summary>
    169.  
    170.     void Jump()
    171.     {
    172.         rb2d.velocity = new Vector2(rb2d.velocity.x, 0f); ;
    173.         rb2d.AddForce(Vector2.up * jumpForce);
    174.         animator.Play("Observer_rainbow_jump");
    175.         state = State.jumping;
    176.     }
    177.  
    178.     /// </summary>
    179.  
    180.  
    181.  
    182.  
    183.  
    184.     private void VelocityState()
    185.     {
    186.         if (state == State.jumping)
    187.         {
    188.  
    189.         }
    190.         else if (Mathf.Abs(rb2d.velocity.x) > Mathf.Epsilon)
    191.         {
    192.             state = State.running;
    193.         }
    194.         else
    195.         {
    196.             state = State.idle;
    197.         }
    198.  
    199.     }
    200.  
    201.     void Die()
    202.     {
    203.         Application.LoadLevel(Application.loadedLevel);
    204.     }
    205.  
    206.     public void Damage(int dmg)
    207.     {
    208.         curHealth -= dmg;
    209.     }
    210. }
     
  2. the_0dium

    the_0dium

    Joined:
    Nov 29, 2018
    Posts:
    15
    You are using GetKey for a jump button. It works for walking but shouldn't be used for jumping because it is being read every frame. You press a jump key and code applies upwards force. Because of gravity your character slows down and at some point velocity.y becomes 0 in mid air (right before the character starts falling) which leads to onTheGround becoming true. Because you are using GetKey and you are holding a key pressed, a jump happens mid-air. Use GetKeyDown for a jump. Also I think you should better use isGrounded to determine if you are on the ground instead.
     
    Deleted User likes this.