- Home /
how to convert the angle to roatation?
hai guys....am trying to move the vehicle based on the directional vectors...when am try to translate it works quite well but when try to rotate it doesnt perform well.....this is my concept....i have the two directional vectors using this i found the angle how much the vehicle want to rotates(using vector3.angle). using this angle how can i supposed to rotate??? i dont know how to convert the signedAngle value into the rotations....i just read somewhere using quaterinion we can rotate but i dont know...please give some practical explanation with the example code.....Thank u...
Answer by robertbu · Sep 26, 2014 at 02:39 PM
Vector3.Angle() does not help you since the result is unsigned. Given the forward vector of your vehicle and a vector indicating where you want it to point you can create a Quaternion:
var q = Quaternion.FromTo(myForward, nextDirection);
...then you can set the rotation immediately by:
transform.rotation = q * transform.rotation;
...or you can do it over time with easing by:
transform.rotation = Quaternion.Slerp(transform.rotation, q * transform.rotation, Time.deltaTime * speed);
For a vehicle, you sometimes want to force the angle of rotation. Assuming you've defined the front of your vehicle as the side facing positive 'z' when the rotation is (0,0,0), you can use Quaternion.LookRotation():
var q = Quaterion.LookRotation(nextDirection, transform.up);
...and the Slerp would be:
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
Note for a non-eased rotation, Slerp() can be replace by RotateTowards(). 'speed' would then be measured in degrees per second and would have to be greatly increased from the value used in Slerp().
hai mr.robertu ......actually i have the signed ange between two directional vector....using that angle how can i achieve the rotation??? is there possible to rotate the car using angle???
In addition to the signed angle, you need the axis. If you have the axis, you can do:
transform.rotation = Quaternion.AngleAxis(angle, axis) * transform.rotation;