如何将对象的旋转设置为在Unity中跟随一条线?

huangapple go评论76阅读模式
英文:

How to set an objects rotation to follow a line in unity?

问题

我有两个向量形成一条线,我想将物体放置在其中一个向量上,并使其旋转以跟随这个角度。

我尝试了Unity中大部分我能找到的函数,但始终无法正确实现。

英文:

I have 2 vectors that form a line and I want to place the object at one of the vectors, with a rotation that follows this angle.

I have tried using most of the functions in unity that I can find but cannot get it right at all.

答案1

得分: 0

你可以使用Quaternion.LookRotation来计算旋转。只需为两点之间的向量和一个“上方向”(可能是Vector3.up)提供参数。

var rotation = Quaternion.LookRotation(p1 - p2, Vector3.up);

或者,由于Vector3.up已经是默认值

var rotation = Quaternion.LookRotation(p1 - p2);
英文:

You should be able to use Quaternion.LookRotation to compute a rotation. Just give it a vector for the vector between the two points, and a "up-direction", probably Vector3.up.

var rotation = Quaternion.LookRotation(p1 - p2, Vector3.up);

or - since Vector3.up is the default value anyway

var rotation = Quaternion.LookRotation(p1 - p2);

答案2

得分: 0

以上帝之名。以下命令将解决您的问题。

transform.position = v1;
transform.forward = (v2 - v1).normalized;
英文:

in the name of God. The following command will solve your problem.

transform.position = v1;
transform.forward = (v2 - v1).normalized;

huangapple
  • 本文由 发表于 2023年7月24日 17:46:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定