Unity Lerp和Slerp动作不流畅 [英] Unity Lerp and Slerp not moving smoothly

查看:623
本文介绍了Unity Lerp和Slerp动作不流畅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个协程以处理相机的移动和外观方向,在此协程中,我有:

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;
    }
}

问题是,当我实际上在进行捆扎和捆扎时,它看起来很糟糕,而且从未如此平滑.我花了几天时间试图弄清楚这一点,对此我感到困惑.我查看了 Lerp and Slerp Question ,发现这是几乎是相同的问题,但是这个人需要的答案已经有了.

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.

推荐答案

这会更像这样

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
  }
}

首先测试一下POSITION,然后尝试扭转!干杯

At first test it with JUST lerping the POSITION, then try the twist! Cheers

这篇关于Unity Lerp和Slerp动作不流畅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆