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…
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().