首页 > 解决方案 > 在unity3d中插值运动时的抖动运动

问题描述

基本上,我的 fps 播放器上有两个枪物。真枪和玩家看到的枪。我在枪上有一个脚本,玩家看到它通过插值跟随作为相机子级的真枪的位置和旋转。

旋转很好,但是随着枪的位置,我会感到紧张不安。这是我在玩家看到的枪上的平滑移动脚本:

public Transform target; //The gun that the player doesn't see
public float moveSpeed = 0.125f;
public float rotateSpeed = 0.125f;

void LateUpdate()
{
    //this is causing the jittery motion
    transform.position = Vector3.Lerp(transform.position, target.position, moveSpeed);

    //this is very smooth
    transform.rotation = Quaternion.Lerp(transform.rotation, target.rotation, rotateSpeed);
}

有人知道为什么吗?

标签: unity3dinterpolation

解决方案


编辑:否则,我会为你的 moveSpeed 使用更明智的单位,这在你的世界中是有意义的,例如 1 米,然后我会使用 Time.deltaTime 来平滑运动。如果它太慢或太快,您可以调整速度单位。

[SerializeField] private float moveSpeed = 1f;

void LateUpdate()
{
    var t = moveSpeed * Time.deltaTime; //< just to highlight

    transform.position = Vector3.Lerp(transform.position, target.position, t);
}

使用 Time.deltaTime 的实际原因是根据您的帧速率帮助平滑,如果您获得较低的帧,Lerp 将进行调整。Unity 论坛的 Eric5h5 在这个答案中也解释了这种常用方法。


嗯,我想知道你是否真的需要一个 Lerp 来更新枪的位置,如果你想同步两者,我会更新你的transform.position = target.position,因为它每帧都完成,它们会同步。

添加 lerp 只会延迟同步。

您看到四元数和 Lerp 之间的差异这一事实只是您无法比较这两种速度,我相信您设置的旋转速度比 lerp 的速度快得多。


推荐阅读