【问题标题】:Unity3D face object where its movingUnity3D人脸对象在哪里移动
【发布时间】:2021-11-10 20:49:55
【问题描述】:

所以我做了一个和你一起飞的同伴,它很好: this.transform.position = Vector3.Lerp(transform.position, player.transform.position + offset, speed * Time.deltaTime);

但现在这是它开始的地方......目前我正在使用它,但我希望对象在它移动的地方自行旋转: this.transform.LookAt(player, Vector3.up);

非常感谢您的回答。

祝你有美好的一天。

TobiHudi

【问题讨论】:

  • @derHugo 我认为问题在于追随者指向玩家的方向,而不是追随者的移动方向。由于追随者从玩家那里获得了一些offset,因此使用LookAt 会使其看向玩家,而不是带有额外offset 的对象将向其移动的位置。 OP可以澄清,但这就是我解释这个问题的方式。

标签: c# unity3d transform


【解决方案1】:

你可以用一些向量数学来做到这一点。存储您同伴的先前位置,然后使用新的目标位置来确定方向向量。代码可能类似于:

private void Update()
{
    // store our prev location
    Vector3 prevLocation = this.transform.position;
   
    // update the position as you are already doing
    this.transform.position = Vector3.Lerp(transform.position, player.transform.position + offset, speed * Time.deltaTime);
   
    // now calculate our direction using the targetLoc - prevLoc
    // assure to normalize so we have a non scaled vector, which is just a direction
    Vector3 newDir = (this.transform.position - prevLocation).normalized;

    // assign the rotation of our object to the direction we are now moving
    this.transform.rotation = Quaternion.LookRotation(newDir);
}

上述 sn-p 未经测试,但基于 Quaternion.LookRotation 文档,因此它应该可以按预期工作。

【讨论】:

  • 谢谢!我非常感谢您的反馈! (它确实有效......不过......你花时间帮助我真是太棒了)
  • @TobiHudi 很高兴我能帮上忙!如果此帖子回答了您的问题,您可以通过单击帖子左侧的复选标记接受答案来关闭问题。它让其他人知道这个问题有一个有效的答案。
猜你喜欢
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多