Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Floaty Jump

Discussion in 'Getting Started' started by Yegor42069, Feb 23, 2023.

  1. Yegor42069

    Yegor42069

    Joined:
    Dec 5, 2022
    Posts:
    26
    Im modifying my player movement script right now and my player has this floaty jump and its really annoying and I don't know how to get rid of it I've been looking everywhere too fix but i cant here is my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     private Rigidbody2D rb;
    8.     public float speed = 10f;
    9.     private bool IsJumping;
    10.     bool facingRight = true;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         float dirX = Input.GetAxisRaw("Horizontal");
    21.         rb.velocity = new Vector2(dirX * speed, rb.velocity.y);
    22.         //Without dirx the 7 will be set to a negative value which would make the player only go left
    23.         if(dirX<0 && facingRight)// if you press the left arrow and your facing right then you will face left
    24.          {
    25.             flip();
    26.          }
    27.         else if (dirX > 0 && !facingRight)// if you press the right arrow and your facing right then you will face right
    28.         {
    29.             flip();
    30.         }
    31.  
    32.         if (Input.GetButtonDown("Jump") && !IsJumping)
    33.         {
    34.             rb.velocity = new Vector3(rb.velocity.x, 6, 0);
    35.             IsJumping = true;
    36.         }
    37.     }
    38.  
    39.     private void flip()
    40.     {
    41.         facingRight = !facingRight;
    42.         transform.Rotate(0f, 180f, 0f);
    43.     }
    44.  
    45.  
    46.  
    47.     private void OnCollisionEnter2D(Collision2D other)
    48.     {
    49.         if (other.gameObject.CompareTag("Ground"))
    50.             IsJumping = false;
    51.     }
    52. }
    53.