Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

rigidbody.velocity.y problem

Discussion in 'Editor & General Support' started by ViktorEckard, Jun 1, 2020.

  1. ViktorEckard

    ViktorEckard

    Joined:
    Apr 9, 2020
    Posts:
    2
    Hello!

    I am having a problem with my vertical velocity.

    Basically, I want to see if my character's up and down velocity is changing to know if it is jumping. The problem is that, even though my character is not moving in the y axis (up and down), the y velocity is still has a value which ranges at around 9. I know this because I am using [SerializeField] on a Vector3 that every update is equalized to rigidbody.velocity.y. I can also see that the character's y position is the same always. I am not modifying the velocity variable anywhere in my code.

    Have I understood velocity wrong? Should I retake physics class? What could my problem be?

    Any help appreciated.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    I can't say I know why your game is reporting a velocity when there is none. However, velocity is not a great way to tell if your character is jumping in any case. One reason is that at the very height of your jump, your character's velocity will be zero (or close to it). The typical way people do this is keep an "isGrounded" boolean variable around, and update it in OnCollisionEnter() and OnCollisionExit() when the other collider is the ground.
     
    ViktorEckard likes this.
  3. ViktorEckard

    ViktorEckard

    Joined:
    Apr 9, 2020
    Posts:
    2
    Thanks for the tip! The problem is that my player is hovering above the ground always. I think I will change some stuff with the movement. I realize that it won't work in the long run.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    Ah, that is tricky! Nevertheless a grounded check or a "nearly grounded" check can be done either by way of trigger colliders that are larger than your character's model or downward-facing raycasts to measure distance to the ground.