Search Unity

Losing momentum while jumping

Discussion in 'Scripting' started by Amn1C, Oct 16, 2019.

  1. Amn1C

    Amn1C

    Joined:
    Sep 29, 2019
    Posts:
    10
    Hello all,

    I have this code to allow running and jumping :

    Code (CSharp):
    1.     private void Run()
    2.     {
    3.         float xInputDirection = Input.GetAxis("Horizontal");
    4.         Vector2 runMovement = new Vector2(xInputDirection, 0f);
    5.  
    6.         playerRigidBody.AddForce(runMovement * runPower, ForceMode2D.Force);
    7.     }
    8.     private void Jump()
    9.     {
    10.         if (jumpButtonPressed && playerTouchedGround)
    11.         {
    12.             playerRigidBody.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
    13.         }
    14.             jumpButtonPressed = false;
    15.     }
    It works, but not perfectly. Although I'm using forces I'm experiencing:
    1. A small decrease in velocity when jumping
    2. A decrease in velocity when falling (or being in the air in general).
    I think it would be kind of stupid adding a manual force to the direction I'm jumping at to account for the loss, since I'm heavily relying on the physics engine.

    The velocity reduction while falling includes drag put to 0, and different values tested for gravity which don't fix the problem.

    Since I'm using forces I assume they only apply to the axis I'm targetting ? Any ideas where I went wrong or how to solve this ?

    Thanks in advance
     
  2. Amn1C

    Amn1C

    Joined:
    Sep 29, 2019
    Posts:
    10
    I managed to fix the decrease in velocity when jumping, though am still experiencing a lot of decrease when falling, even with drag set to 0.

    Any ideas ?