I got a 2D Player object. This object always looks towards his moving direction with this code:
float direction = Mathf.Atan2(rb.velocity.y, rb.velocity.x)* Mathf.Rad2Deg
transform.rotation = Quaternion.AngleAxis(direction , Vector3.forward);
For the most part this rb.velocity returns the right direction. But when the player collides with another object the direction isnt as accurate as it should be. On the screenshot u can see in which direction the player is moving based on his trail, but u can also see that he is facing a wrong direction
Is there another way to get the direction without using rb.velocity ?
I’m confused here a little, it seems you’re trying to indicate that 2D physics is broken somehow with something as basic as it reporting the body velocity? Is that what you’re saying?
Velocity IS a direction. It moves in that direction because the velocity is correct so I’m confused. You show expected and actual velocity. What does that mean? Where is the actual coming from? You can see the body velocity in the inspector by looking at the Rigidbody2D > Info.
Maybe you’re saying your calculation of angle and subsequent rotation is incorrect. If that’s the case then maybe you should not try to state that instead of saying rb.velocity is incorrect and if there’s a non-broken alternative.
Can you please clarify here please.
I’ve noticed your posts start with an assumption that Unity has a bug.
Mathf.Atan2(rb.velocity.y, rb.velocity.x)* Mathf.Rad2Deg
Are you sure you know what this does for any vector (velocity)?
Velocity Right (1, 0) = Rotation of 0
Velocity Up (0, 1) = Rotation of +90
Velocity Left (-1, 0) = Rotation of +180
Velocity Down (0, -1) = Rotation of -90
I presume you know these are the rotations you’ll get because you’ve written the code.
Are you sure you don’t want it so that when moving “up” you have no rotation? If so then you use:
Mathf.Atan2(rb.velocity.x, rb.velocity.y)* Mathf.Rad2Deg
This gives:
Velocity Right (1, 0) = Rotation of +90
Velocity Up (0, 1) = Rotation of 0
Velocity Left (-1, 0) = Rotation of -90
Velocity Down (0, -1) = Rotation of +180
Yes i understand that velocity is a direction, thats why i used it to face the player in the direction in which hes moving. For the most part it does work as expected. But when the player collides with another object and bounces of, the velocity vector doesn’t return the velocity vector i would expect. On the screenshot u can see in which direction the player is moving according to his trail, but u can also see that the player is facing the wrong direction. Therefor the velocity vector doesn’t actually return the velocity in which the player is moving, after it bounces of another object
TBH your image isn’t clear on what it’s showing, it is to you though obviously. Again, you’re saying the velocity isn’t correct but that’s the velocity the physics system is using so how can it be wrong or unexpected? I put a lot of detail above saying how you can calculate the direction but you seem to have skipped past all that.
This doesn’t mean the velocity is wrong! Can you see that it likely means you’re simply calculating the direction wrong from that velocity as said above?
It seem you’re stuck on saying that the physics system is broken somehow on something so fundamental. I mean the title of your post is “Rigidbody2d.velocity not correct”!
Maybe show some numbers of the velocity before/after collision and the direction you calculate. This would be better than some image with hand draw vectors.
ok maybe my screenshot wasnt as clear as i thought. Lets try again.

The position of the blue sphere is the visualization of the velocity. Its position is set by following code velocityPos.position = (Vector2 )player.transform.position + player.rb.velocity.normalized;
Because i use velocity vector to determine the direction in which the player is oriented, the player always looks in the direction of the blue sphere.
Under Info in the inspector u can also see the value of velocity.
I would assume that the velocity vector would point in the direction the player is actually moving, but as you can see its pointing almost in a -90 degree, while the player is actually moving in a degree more like -45, based on the trail.
I cannot see any of your scripts so I don’t know:
-
Is the Rigidbody2D here moving under its own velocity and you’re NOT modifying the transform.position or otherwise modifying the Rigidbody?
-
Are the visual of the player on a separate GameObject to the Rigidbody2D and not a child?
-
The rotation of the body is 177. Why is it rotated? Are you explicitly rotating it? If so, why is the rotation not constrained in Z because you don’t want the physics system to change it do you?
-
The Rigidbody2D is rotating 1382 degrees/sec. Why?
Do you have a super simple reproduction project you can send to me? If you want me to host it, send me a DM with your email and I’ll setup a place for you to host it privately.