Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Gravity messing with horizontal momentum

Discussion in 'Scripting' started by Amn1C, Nov 8, 2019.

  1. Amn1C

    Amn1C

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

    When my player object is falling and I apply no friction, gravity causes it to slow down drastically.
    As far as I understand, gravity shouldn't do this right ?

    I have the following code to move my object:

    Code (CSharp):
    1.         float xInputDirection = Input.GetAxis("Horizontal");
    2.         Vector2 runVector = new Vector2(xInputDirection, 0f);
    3.  
    4.         playerRigidBody.AddForce(runVector * runPower, ForceMode2D.Force);
    When I apply friction, it causes my player object to fall slower, and increases the horizontal momentum..

    It's also worth noting Update() gives better results than Fixedupdate() which is what I want to avoid using. Does anyone have any idea why this occurs ?

    Thanks in advance.
     
  2. MitjaHD

    MitjaHD

    Joined:
    Sep 30, 2018
    Posts:
    64
    Try this:

    Code (CSharp):
    1. Vector2 runVector = new Vector2(xInputDirection,  playerRigidBody.velocity.y);
    You were setting y velocity to zero (I'm guessing the code was inside Update/FixedUpdate).