Search Unity

consistent speed while jumping (jump and run game)

Discussion in 'Physics' started by AAppDev, Aug 23, 2015.

  1. AAppDev

    AAppDev

    Joined:
    Aug 21, 2015
    Posts:
    17
    Hi I work on a simple jump and run game but I have the problem that when I jump the player stops.
    I don't know why because I add in jump() the x movement.
    Code (CSharp):
    1. void run(){
    2.         rb2d.AddForce (new Vector2 (moveForce, 0f));
    3.     }
    4.  
    5.     void jump(){
    6.         rb2d.AddForce (new Vector2 (moveForce, jumpForce));
    7.         jumping = true;
    8.     }
     
  2. AAppDev

    AAppDev

    Joined:
    Aug 21, 2015
    Posts:
    17
    I solved the problem by using RigidBody2d.velocity instead RigidBody2D.AddForce

    Code (CSharp):
    1.     void run(){
    2.         jumping = false;
    3.         rb2d.velocity = new Vector2(moveSpeed, rb2d.velocity.y);
    4.     }
    5.  
    6.     void jump(){
    7.         jumping = true;
    8.         rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
    9.         rb2d.velocity = new Vector2(rb2d.velocity.x, jumpSpeed);
    10.     }