Smooth Rotation Quaternion Lerp?
Hey Guys! I'm new to coding in Unity and I'm attempting to make a script in which the ai runs around within an arena with a constant forward direction while changing to random y rotations. I had an old version of the script in which the characters would snap to a new y rotation. That worked fine but I wanted to make it so that they smoothly swivel to the new randomly generated value. Working with Quaternions is making my head explode a little bit and I was wondering if anyone could possibly help me resolve my issue. The character does slightly swivel right now but quickly gets stuck between the values of 0 and 180.
Answer by toromano · Oct 19, 2015 at 11:30 AM
Modifying Euler angle components directly can cause problems. Try Using this in your randomRotation function:
Vector3 RandomDirection = new Vector3(Random.Range(-1.0f, 1.0f), .0f, Random.Range(-1.0f, 1.0f));
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(RandomDirection), currentLerpTime / lerptime);
Using
rotateAngle.y = Random.Range( -180.0f,180.0f);
might solve your problem too.