Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Jump mechanic not working properly

Discussion in '2D' started by PandaVibes, May 17, 2024.

  1. PandaVibes

    PandaVibes

    Joined:
    May 17, 2024
    Posts:
    1
    So I'm new to unity, and coding as a whole really, so I've been trying to recreate some simple games. This included a 2D platformer, which the left/right movement was pretty easy but my jump is weird. I was able to get the jumping done pretty quickly but I've been struggling with having the player only be able to jump on the ground. I thought of something where if your Y velocity was 0, you could jump. Since your Y velocity isn't changing on the ground, but is when your in the air, you should only be able to jump on the ground. This code led to the player only being able to jump sometimes (seems random when they can jump) and only when standing still. I've been trying to fix this for a while now, but I'm stuck. Anyone think they can help? (again, I am new, so I probably will ask a couple of questions lol)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5. using UnityEngine.UIElements;
    6.  
    7. public class Movement : MonoBehaviour
    8. {
    9.     public Rigidbody2D myRigidBody;
    10.     public float speed = 10;
    11.     public float jumpHeight = 5;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.        
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         // Right movement
    23.         if (Input.GetKey(KeyCode.D))
    24.         {
    25.             myRigidBody.velocityX = speed;
    26.         }
    27.         if (Input.GetKeyUp(KeyCode.D))
    28.         {
    29.             myRigidBody.velocity = Vector2.zero;
    30.         }
    31.  
    32.         // Left movement
    33.         if (Input.GetKey(KeyCode.A))
    34.         {
    35.             myRigidBody.velocityX = -speed;
    36.         }
    37.         if (Input.GetKeyUp(KeyCode.A))
    38.         {
    39.             myRigidBody.velocity = Vector2.zero;
    40.         }
    41.  
    42.         // Jump
    43.         if (Input.GetKeyDown(KeyCode.W))
    44.         {
    45.             if (myRigidBody.velocityY == 0)
    46.             {
    47.                 myRigidBody.velocity = Vector2.up * jumpHeight;
    48.             }
    49.         }
    50.     }
    51. }
    52.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,412
    Plenty of existing examples to start from on the interwebs.

    While you could use y==0 as your can jump criteria, there are usually better solutions.

    For instance, using y==0 means if you are running on sloped / curved ground you can't jump.


    Keep in mind also never to compare floating point numbers for equality.

    Here's why: Floating (float) point imprecision:

    Never test floating point (float) quantities for equality / inequality. Here's why:

    https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

    https://forum.unity.com/threads/debug-log-2000-0f-000-1f-is-200.1153397/#post-7399994

    https://forum.unity.com/threads/why-doesnt-this-code-work.1120498/#post-7208431

    "Think of [floating point] as JPEG of numbers." - orionsyndrome on the Unity3D Forums





    Here's my 2D jump platform code example, including coyote time and jump buffering and multijump... full source in comments:

     
    Last edited: May 17, 2024