简体   繁体   English

Unity Lerp和Slerp运动不顺畅

[英]Unity Lerp and Slerp not moving smoothly

I created a Coroutine to handle my camera movement and direction to look and in this coroutine I have : 我创建了一个Coroutine来处理我的相机移动和方向,我在这个协程中有:

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. 问题在于,当我实际上是Lerping和Slerping时,它看起来很糟糕而且并不光滑。 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. 我看了Lerp和Slerp问题 ,看到这几乎是同样的问题,但是我已经拥有的人需要的答案。

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! 首先用JUST测试它,然后尝试扭转! Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM