Search Unity

Infinite Jumping Bug

Discussion in 'Physics' started by SomethingToDoWithEverything, Oct 20, 2020.

  1. SomethingToDoWithEverything

    SomethingToDoWithEverything

    Joined:
    Oct 20, 2020
    Posts:
    12
    While testing my game, I noticed that my jumping wouldn't end. I was wondering how I could fix this. Here's the script i'm using
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerController2D : MonoBehaviour {
    7. Animator animator;
    8. Rigidbody2D rb2d;
    9. SpriteRenderer spriteRenderer;
    10.  
    11.  
    12.  
    13.     void Start() {
    14.  
    15.     animator = GetComponent<Animator>();
    16.     rb2d = GetComponent<Rigidbody2D>();
    17.     spriteRenderer = GetComponent<SpriteRenderer>();
    18.  
    19.  
    20.      
    21.     }
    22.  
    23.  
    24.  
    25.     private void FixedUpdate()
    26.     {
    27.  
    28.        if(Input.GetKey("d") || Input.GetKey("right"))
    29.        {
    30.             rb2d.velocity = new Vector2(2, rb2d.velocity.y);
    31.             animator.Play("Player Run");
    32.        }
    33.        else if(Input.GetKey("a") || Input.GetKey("left"))
    34.        {
    35.           rb2d.velocity = new Vector2(-2, rb2d.velocity.y);
    36.        
    37.           animator.Play("Player Run");
    38.  
    39.  
    40.        }
    41.        else
    42.        {
    43.         animator.Play("Player idle");
    44.  
    45.  
    46.        }
    47.  
    48.        if(Input.GetKey("space"))
    49.        {
    50.             rb2d.velocity = new Vector2(rb2d.velocity.x, 3);
    51.             animator.Play("Player Jump");
    52.  
    53.  
    54.        }
    55.    
    56.     }
    57.  
    58.      
    59. }
    60.  
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Did you mean to use GetKeyDown instead of GetKey for your jumping?