【问题标题】:How to rotate the camera smoothly in Unity?如何在 Unity 中平滑旋转相机?
【发布时间】:2017-09-22 11:38:10
【问题描述】:

我有一个 3D 汽车游戏,其中相机从上到下指向。当汽车移动时,我需要相机跟随它。我知道如何进行位置跟随,但我不知道如何顺利​​地进行与汽车相同的旋转。我只需要更改y 轴。 x = 90z = 0 一直都是。汽车也只在y 轴上旋转。

public float interpVelocity;
public float minDistance;
public float followDistance;
public GameObject target;
public Vector3 offset;
Vector3 targetPos;

// Use this for initialization
void Start()
{
    targetPos = transform.position;
}

// Update is called once per frame
void FixedUpdate()
{
    if (target)
    {
        Vector3 posNoZ = transform.position;
        posNoZ.z = target.transform.position.z;

        Vector3 targetDirectionZ = (target.transform.position - posNoZ);

        interpVelocity = targetDirectionZ.magnitude * 5f;

        targetPos = transform.position + (targetDirectionZ.normalized * interpVelocity * Time.deltaTime);

        transform.position = Vector3.Lerp(transform.position, targetPos + offset, 0.25f);


        Vector3 posNoX = transform.position;
        posNoX.x = target.transform.position.x;

        Vector3 targetDirectionX = (target.transform.position - posNoX);

        interpVelocity = targetDirectionX.magnitude * 5f;

        targetPos = transform.position + (targetDirectionX.normalized * interpVelocity * Time.deltaTime);

        transform.position = Vector3.Lerp(transform.position, targetPos + offset, 0.25f);
    }
}

【问题讨论】:

标签: c# unity3d camera


【解决方案1】:

也许Mathf.Lerp 对您的情况有所帮助。角度的 Lerp 与 Vector 的 lerp 的工作方式相同,但在环绕 360 度时也要记住正确的值。如果您想始终保持 x 和 z 轴相同,您的角度计算将如下所示:

var currentAngle = new Vector3( 90.0f,
         Mathf.LerpAngle(currentAngle.y, targetAngle.y, Time.deltaTime), 0.0f);

【讨论】:

  • 对不起,我不明白。你能写出完整的部分吗?我将此添加到结尾,但它仍然无法正常工作。 transform.Rotate(new Vector3(90.0f, Mathf.LerpAngle(transform.rotation.y, target.transform.rotation.y, Time.deltaTime), 0.0f));
猜你喜欢
  • 2023-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多