Unity 3D 制作 Objects 关注玩家
Unity 3D Make Objects Follow Player
我正在尝试让 object 无延迟地跟随玩家并在 Z-axis 上进行偏移但在 X-axis 上延迟。当玩家输入 object(硬币)触发器时,我将此 object 添加到列表中,并使用此代码更新每个人的位置。每个硬币都有当前硬币跟随的上一个和下一个硬币。
{
foreach (Coin coin in coins)
{
Vector3 desiredPos = new Vector3(
coin.previous.transform.position.x,
coin.previous.transform.position.y,
coin.previous.transform.position.z + .15f);
Vector3 smoothedPos = Vector3.Lerp(
coin.transform.position,
desiredPos,
smoothness);
coin.transform.position = smoothedPos;
}
}
我想得到这个结果
https://youtube.com/shorts/PHAg8Pqf0mA?feature=share
这就是我得到的
https://youtube.com/shorts/fzukzL_0r74?feature=share
但是正如您所看到的,硬币有时会挣扎。它没有正确更新他们的位置。我不想做到 child 因为我需要延迟 x-axis 移动它们。
如果你能帮助我,我将不胜感激。
您正在使用 Vector3.Lerp 对所有 x y 和 z 进行 lerps。如果你只想在 x 轴上 lerp,你应该在 x 上使用 Mathf.Lerp 代替:
var x = Mathf.Lerp(x, desiredPos.x, 平滑度);
我正在尝试让 object 无延迟地跟随玩家并在 Z-axis 上进行偏移但在 X-axis 上延迟。当玩家输入 object(硬币)触发器时,我将此 object 添加到列表中,并使用此代码更新每个人的位置。每个硬币都有当前硬币跟随的上一个和下一个硬币。
{
foreach (Coin coin in coins)
{
Vector3 desiredPos = new Vector3(
coin.previous.transform.position.x,
coin.previous.transform.position.y,
coin.previous.transform.position.z + .15f);
Vector3 smoothedPos = Vector3.Lerp(
coin.transform.position,
desiredPos,
smoothness);
coin.transform.position = smoothedPos;
}
}
我想得到这个结果 https://youtube.com/shorts/PHAg8Pqf0mA?feature=share
这就是我得到的
https://youtube.com/shorts/fzukzL_0r74?feature=share
但是正如您所看到的,硬币有时会挣扎。它没有正确更新他们的位置。我不想做到 child 因为我需要延迟 x-axis 移动它们。
如果你能帮助我,我将不胜感激。
您正在使用 Vector3.Lerp 对所有 x y 和 z 进行 lerps。如果你只想在 x 轴上 lerp,你应该在 x 上使用 Mathf.Lerp 代替:
var x = Mathf.Lerp(x, desiredPos.x, 平滑度);