英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论