【问题标题】:Unity 3D Make Objects Follow PlayerUnity 3D 使对象跟随玩家
【发布时间】:2022-01-17 03:52:54
【问题描述】:

我试图让物体跟随玩家,在 Z 轴上没有延迟,但在 X 轴上延迟。当玩家输入对象(硬币)触发器时,我将此对象添加到列表中,并使用此代码更新每个人的位置。每个硬币都有当前硬币跟随的上一个和下一个硬币。

{
    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

但正如您所见,代币有时会挣扎。它没有正确更新他们的位置。我不想让它成为孩子,因为我需要在 x 轴上延迟移动它们。

如果你能帮助我,我将不胜感激。

【问题讨论】:

  • 请使用正确的标签!请注意,unityscript 是或更好地曾经是一种 JavaScript 风格,类似于早期 Unity 版本中使用的自定义语言,并且现在已经不推荐使用了!您的代码显然在c# ..

标签: c# unity3d game-engine


【解决方案1】:

您正在使用 Vector3.Lerp,它会处理所有 x y 和 z。如果您只想在 x 轴上进行 lerp,则应在 x 上使用 Mathf.Lerp:

var x = Mathf.Lerp(x, desiredPos.x, 平滑度);

【讨论】:

  • 感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多