Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Jump too Teleporty

Discussion in 'Getting Started' started by Jabbsy7, Apr 20, 2024.

  1. Jabbsy7

    Jabbsy7

    Joined:
    Apr 16, 2024
    Posts:
    1
    So I'm extremely new to unity and my character jump works, however, the actual jump is just teleporting upward. How would I make this a fluid up and down motion rather than teleporting up and falling down with gravity?
    Thanks!
    Code (CSharp):
    1. {
    2.     public float speed;
    3.     public Rigidbody2D rb;
    4.     private float movement;
    5.     public float jump;
    6.  
    7.  
    8.    
    9.     void Start()
    10.     {
    11.        
    12.     }
    13.  
    14.  
    15.     void Update()
    16.     {
    17.      
    18.  
    19.        
    20.         if (Input.GetKeyDown(KeyCode.Space))
    21.         {
    22.            
    23.             rb.AddForce(new Vector2(rb.velocity.x, jump));
    24.         }
    25.         {
    26.             movement = Input.GetAxisRaw("Horizontal");
    27.  
    28.         }
    29.        
    30.         rb.velocity = new Vector2 (movement * speed, 0 * Time.deltaTime);  
    31.     }
    32. }
     
  2. DrDemencio

    DrDemencio

    Joined:
    Sep 2, 2022
    Posts:
    82
    I guess you mean it looks like you rigibdbody just teleports some place in the air and the just starts falling right? I think the problem must be you're passing too high values to AddForce. I say this because I see you're passing rb.velocity.x in the X component, and that's not really a comparable magnitude (unless you use the ForceMode.VelocityChange mode). You could try just setting the initial vertical jump speed. Something like:
    rb.velocityY = jump;


    Another problem I see here is you're both adding a force and modifying velocity. In this line:
    rb.velocity = new Vector2 (movement * speed, 0 * Time.deltaTime);

    You're setting rb.velocity.y to 0 in every frame (which doesn't mean it will fully stop, because gravity will still move you down between frames). If you just want to move horizontally I suggest writting something like:
    rb.velocityX = movement * speed
     
    Ryiah likes this.