在 unity3d 中插值运动时的抖动
Jittery motion when interpolating movement in 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);
}
有人知道为什么吗?
编辑:
否则,我会为你的移动速度选择一个更合理的单位,这在你的世界中是有意义的,例如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 将进行调整。这个常用方法也有解释in this answer by Eric5h5 from Unity forum.
嗯,我想知道你是否真的需要 Lerp 来更新枪的位置,如果你想同步两者我会更新你的 transform.position = target.position
因为它已经完成每一帧,它们会同步。
添加 lerp 只会延迟同步。
事实上,您看到四元数和 Lerp 之间的区别只是您无法比较这两个速度,我相信您设置的旋转速度比 lerp 快得多。
基本上,我的 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);
}
有人知道为什么吗?
编辑: 否则,我会为你的移动速度选择一个更合理的单位,这在你的世界中是有意义的,例如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 将进行调整。这个常用方法也有解释in this answer by Eric5h5 from Unity forum.
嗯,我想知道你是否真的需要 Lerp 来更新枪的位置,如果你想同步两者我会更新你的 transform.position = target.position
因为它已经完成每一帧,它们会同步。
添加 lerp 只会延迟同步。
事实上,您看到四元数和 Lerp 之间的区别只是您无法比较这两个速度,我相信您设置的旋转速度比 lerp 快得多。