问题
I created a Coroutine to handle my camera movement and direction to look and in this coroutine I have :
public IEnumerator MoveCameraLookAtObject(Transform _cameraTransform, Vector3 startPos, Vector3 endPos, Vector3 lookAt, float time)
{
// Loop through a timed based situation.
for (float i = 0f; i <= 1.0f; i += Time.deltaTime / time)
{
// Lerp the Movement.
_cameraTransform.position = Vector3.Lerp(startPos, endPos, i);
// Slerp the rotation.
Vector3 relativePos = lookAt - _cameraTransform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
_cameraTransform.rotation = Quaternion.Slerp(_cameraTransform.rotation, rotation, i);
}
yield return null;
}
}
The problem is that when I am actually Lerping and Slerping it looks awful and not smooth what so ever. I am confused on what I am doing wrong as I have spent days trying to figure this out. I looked at Lerp and Slerp Question and saw that this is pretty much the same problem but the answer that person needed I already have.
回答1:
it would be something more like this
public IEnumerator MoveCameraLookAtObject(
Transform _cameraTransform,
Vector3 startPos, Vector3 endPos, Vector3 lookAt, float time)
{
float totalTime = 1.25f; // or whatever
float startTime = Time.time;
float endTime = startTime + totalTime;
startPos = etc etc
endPos = etc etc
startROTATION = ...
endROTATION = ...
// calculate those ONLY OUT HERE!!! not in the loop
while (Time.time < endTime)
{
float timeSoFar = Time.time - startTime;
float fractionTime = timeSoFar/totalTime;
//do NOT USE underscores in variable names
cameraTransform.position =
Vector3.Lerp(startPos, endPos, fractionTime);
cameraTransform.rotation =
Quaternion.Slerp(startROTATION, endROTATION, fractionTime);
yield return null; // this goes IN HERE
}
}
At first test it with JUST lerping the POSITION, then try the twist! Cheers
来源:https://stackoverflow.com/questions/35845187/unity-lerp-and-slerp-not-moving-smoothly