- Home /
Rigidbody2D.velocity.y value not matching Rigidbody2D.velocity value for the Y plan
...
MyRigidbody.velocity = new Vector2(horizontal * movementSpeed, MyRigidbody.velocity.y);
Debug.Log("Ridigbody2D X velocity: " + MyRigidbody.velocity.x);
Debug.Log("Ridigbody2D Y velocity: " + MyRigidbody.velocity.y);
Debug.Log("Ridigbody2D X & Y velocity: " + MyRigidbody.velocity);
...
The code above always yields a Rigidbody2D.velocity.x value that matches the Rigidbody2D.velocity X value, but the Rigidbody2D.velocity.y value does not match the Rigidbody2D.velocity Y value while applying the new velocity. See console output below:
Ridigbody2D X velocity: -1.491004
Ridigbody2D Y velocity: -3.72529E-09
Ridigbody2D X & Y velocity: (-1.5, 0.0)
Ridigbody2D X velocity: -1.996178
Ridigbody2D Y velocity: -1.862645E-09
Ridigbody2D X & Y velocity: (-2.0, 0.0)
Any ideas why they are not matching? It obviously is not due to rounding.
Answer by tanoshimi · Aug 03, 2016 at 08:06 PM
"It obviously is not due to rounding." Well, it is. Debug.Log() is rounding each component of the Vector2 to 1d.p.
In this case alone: Ridigbody2D Y velocity: -3.72529E-09
Ridigbody2D X & Y velocity: (-1.5, 0.0)
-3.anythig is not rounded to 0.0
Did you notice the "E"? It's scientific notation. -3.72529E-09 = 0.00000000372529.
That's pretty damn close to zero.
Sometimes you just look at things too long and miss the forest for the trees.
You can always normalize them too to get it as -1, 0 and 1. go myRigidbody.velocity.normalized.x
and y since they're easier to manipulate, especially since you're already using movementSpeed at your code.
Your answer
Follow this Question
Related Questions
2D Enemy chasing player not at a constant speed,Enemy following player at constant speed 0 Answers
2D Directional top down movement,Topdown 2d Directional Movement 0 Answers
HOLD THE JUMP BUTTON TO JUMP HIGHER 0 Answers
How to Rotate 2d Object with constant speed ? 1 Answer
Moving colliders that are part of a composite collider 1 Answer