- Home /
Question by
Chickenator · Dec 04, 2014 at 04:56 PM ·
rigidbodyrigidbody2daddforcemagnitude
How to find speed in certain direction?
So I know to find rigidbody(2D) speed you do this
Speed = rigidbody2D.velocity.magnitude;
I would like to find out how to do it only to the left. So find out just the speed moving across and not up. Is this possible? Thanks in advance
Comment
Best Answer
Answer by robertbu · Dec 04, 2014 at 04:58 PM
For signed world speed, just do:
var speed = rigidbody2D.velocity.x;
For unsigned world speed, just do:
var speed = Mathf.Abs(rigidbody2D.velocity.x);
If you are looking for local speed, then you will need to transform into local space:
var dir = transform.InverseTransformDirection(rigidbody2D.velocity.magnitude);
speed = dir.x;
...the results will be the speed in the local 'x' direction.
Edit: rereading your question. For only to the left, do:
var speed = rigidbody2D.velocity.x;
if (speed > 0.0) {
speed = 0.0;
}
else {
speed = -speed;
}
This is will give you a positive value for any left movement and 0.0 for any right movement.
when i do this i get rotation values or i get speed even when not moving.