- Home /
Is it possible to obtain velocity relative to rotation?
So I am trying to set an animation float equal my current forward movement. The problem is, velocity isn't relative to the player's rotation. To combat this I have been using this code where "rb" is the rigidbody that I am trying to get the velocity from:
if (rb.velocity.x > 0.1) {
anim.SetFloat ("MoveSpeed", rb.velocity.x);
}
if (rb.velocity.x < -0.1) {
anim.SetFloat ("MoveSpeed", -rb.velocity.x);
}
if (rb.velocity.z > 0.1) {
anim.SetFloat ("MoveSpeed", rb.velocity.z);
}
if (rb.velocity.z < -0.1) {
anim.SetFloat ("MoveSpeed", -rb.velocity.z);
}
This method is proving to be very unreliable though. Is there any way at all to read a rigidbody's velocity relative to it's current rotation?
the Dot product is exactly what does this. Using
Vector2.Dot(rb.right, rb.velocity);
returns the amount of force in right direction of the player. If it's right points right and the force hours orthogonal (up, or down), the result is 0
Your answer
Follow this Question
Related Questions
creating animation made my each prefab spawn at same position 1 Answer
How to make a collision script 1 Answer
How to prevent multiple animations playing at the same time on the same object. 1 Answer
Animation/Scripting 1 Answer
How to trigger animaton on two different Game Object's at the same time? 1 Answer