Quaternion lerp slowing down movement?
I have created a unit class implemented on a gameObject, the unit can move, however I am now integrating rotation and am a bit confused and dense. The way it worked prior was using the commented out LookAt function. But now I've hacked together a rotation solution using lerp, both the rotation AND movement slows down. I thought only slerp was meant to have this effect? I am confused as to why it's affecting the movement too!
Ideally the solution will rotate without speeding up or slowing down, and then move also without speeding up or slowing down. Explanations and code corrections equally welcome (the return statement is meant to stop the movement code further down from being reached if the unit is rotating)
// unit MUST rotate before moving (otherwise forward node checks buggered)
//transform.LookAt(listMovePos[0]);
Vector3 targetDir = listMovePos[0] - transform.position;
float angle = Vector3.Angle(targetDir, transform.forward);
if (angle > 2.0f)
{
Quaternion targetRotation = Quaternion.LookRotation(listMovePos[0] - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, speed * Time.deltaTime);
return;
}
Answer by NornIndustries · May 23, 2016 at 09:17 PM
I eventually discovered that this will work to allow an object to rotate at a constant speed. It had been hiding in the official documentation all along.
// unit MUST rotate before moving (otherwise forward node checks buggered)
Vector3 targetDir = listMovePos[0] - transform.position;
float angle = Vector3.Angle(targetDir, transform.forward);
if (angle > 2.0f)
{
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, speedRotate * Time.deltaTime, 0.0F);
transform.rotation = Quaternion.LookRotation(newDir);
return;
}