Search Unity

Question Jumping doesn't always work

Discussion in 'Getting Started' started by GermanProU, Feb 13, 2024.

  1. GermanProU

    GermanProU

    Joined:
    Feb 13, 2024
    Posts:
    3
    Hello, when I start the game I press space and the character sometimes jumps and sometimes not. I don't understand what this could be connected with. Perhaps the problem is in the script, which checks whether the character is on the ground or in the air. Or maybe there are not enough frame updates?

    Can you tell me how to make it so that every time you press the spacebar the character jumps (if he is standing on the ground)?

    upload_2024-2-13_9-1-6.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     Rigidbody2D rb;
    8.     public float speed;
    9.     public float jumpHeight;
    10.     public Transform groundCheck;
    11.     bool isGrounded;
    12.     Animator anim;
    13.  
    14.     void Start()
    15.     {
    16.         rb = GetComponent<Rigidbody2D>();
    17.         anim = GetComponent<Animator>();
    18.  
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         CheckGround();
    25.         if (Input.GetAxis("Horizontal") == 0 && (isGrounded))
    26.         {
    27.             anim.SetInteger("State", 1);
    28.         }
    29.         else {
    30.             Flip();
    31.             if (isGrounded)
    32.                 anim.SetInteger("State", 2);
    33.         }
    34.     }
    35.  
    36.     void FixedUpdate()
    37.     {
    38.         rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);
    39.         if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
    40.             rb.AddForce(transform.up * jumpHeight, ForceMode2D.Impulse);
    41.            
    42.     }
    43.  
    44.     void Flip()
    45.     {
    46.         if (Input.GetAxis("Horizontal") > 0)
    47.             transform.localRotation = Quaternion.Euler(0, 0, 0);
    48.         if (Input.GetAxis("Horizontal") < 0)
    49.             transform.localRotation = Quaternion.Euler(0, 180, 0);
    50.     }
    51.  
    52.     void CheckGround()
    53.     {
    54.         Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, 0.3f);
    55.         isGrounded = colliders.Length > 1;
    56.         if (!isGrounded)
    57.             anim.SetInteger("State", 3);
    58.     }
    59.  
    60. }
    61.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,899
    The issue is the use of checking input within FixedUpdate. Input is polled on a per-frame basis, while FixedUpdate is sometimes not called between frames, or called multiple times before Update, as it runs as a different interval (it plays catch up with Update, effectively).

    Input should be polled in Update, and if physics-based things need to happen, act upon them in FixedUpdate. You can just cache values to use between them.

    A simple example of the principle:
    Code (CSharp):
    1. public class ExampleBehaviour : Monobehaviour
    2. {
    3.     private bool _jumpQueued = false;
    4.  
    5.     private void Update()
    6.     {
    7.         _jumpQueued = Input.GetKeyDown(KeyCode.Space);
    8.     }
    9.  
    10.     private void FixedUpdate()
    11.     {
    12.         if (_jumpQueued == true)
    13.         {
    14.             // jump
    15.             _jumpQueued = false;
    16.         }
    17.     }
    18. }
    You would of course be doing more like checking if the player is on the ground, etc, in an actual implementation.
     
    Ryiah and arkano22 like this.
  3. GermanProU

    GermanProU

    Joined:
    Feb 13, 2024
    Posts:
    3
    Can I ask you to show, not in an example, but in the code what needs to be changed? I’m only on my 3rd day in Unity and C#, but I really want to make a game.
    Project file.
     
  4. codercurtis

    codercurtis

    Joined:
    Jan 19, 2016
    Posts:
    4
    Just don't check for
    Input.GetKeyDown
    inside of
    FixedUpdate
    . Step 1 is to move it into
    Update
     
    Ryiah and GermanProU like this.
  5. GermanProU

    GermanProU

    Joined:
    Feb 13, 2024
    Posts:
    3
    wow, now he jumps every time, thanks!