Search Unity

[Solved] Velocity is always 0 even when falling

Discussion in 'Physics' started by terrareef27, Feb 17, 2020.

  1. terrareef27

    terrareef27

    Joined:
    Feb 22, 2018
    Posts:
    8
    I have a RigidBody2D on my gameobject. When I jump using addForce and when I fall by the default gravity system, the velocity of the game object always returns 0. I want to check if the game object is falling and run code off that, and I don't know why the velocity is always saying its 0.

    For reference, this if statement never returns true, even when the object clearly is moving in a downwards direction:
    Code (CSharp):
    1. if (rb.velocity.y < 0) //Checks if the player is falling. If they are, increase they speed at which they fall for a smoother feel
    2.         {
    3.             rb.AddForce(Vector2.up * Physics2D.gravity.y * (gravityMultiplyer - 1) * Time.fixedDeltaTime);
    4.             Debug.Log("Falling");
    5.         }
    Any help would be greatly appreciated!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    There must be much more going on than you've posted here because reading a body velocity is the most basic thing we provide. If you pause the project, select the Rigidbody2D and look at it in the Inspector window you'll an "Info" roll-out. Opening that you can see (amongst other stuff) its Linear Velocity. Add a new GameObject with a default Rigidbody2D and do the same. You'll see its linear velocity changing due to gravity.

    So I've no idea why you say you don't hit this code apart from you modifying its velocity somewhere.

    I also don't understand your code. You can change the Rigidbody2D.gravityScale according to whether you're jumping or falling to give you a different feel to jumping/falling. That's what I'm doing in this example.
     
    Last edited: Feb 17, 2020
    terrareef27 likes this.
  3. terrareef27

    terrareef27

    Joined:
    Feb 22, 2018
    Posts:
    8
    Ok so I did some more testing with some of the methods that you suggested and it seems like the velocity is somehow being set to 0 in my playerMovement script. I am not directly setting the velocity anywhere as I heard that it can cause some strange behaviors. I will do some more testing to see what it is.
     
    marvin123725 likes this.
  4. terrareef27

    terrareef27

    Joined:
    Feb 22, 2018
    Posts:
    8
    I feel real dumb right now. Found out it came from the movePosition method that I was using for movement. I had
    Code (CSharp):
    1. rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    in the FixedUpdate method, which was overriding things like velocity. All I have to do is change that and everything should work fine.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    It doesn't cause "strange behaviours", that's misleading info you've been given. It's just that, for example, if you don't take into account what's already in there then you're "overwriting" velocity that others things are changing such as the accelleration due to gravity etc.

    This doesn't modify velocity at all. It stores whatever velocity was set prior to you calling it and restores it after.

    Anyway, glad you got it working.