在unity3d中使圆柱体指向一个对象
我刚刚开始学习Unity3D游戏开发框架。当按下某个键时,我试图使圆柱体“指向”另一个物体。
public GameObject target;
void Update () {
if (Input.GetKeyDown(KeyCode.A)) {
???
}
}
我知道我必须使用目标和圆柱体的位置来改变圆柱体的旋转,但我不知道如何,我认为我还不明白那些四元数是什么。
我真的很感激任何帮助! 谢谢,
曼努埃尔
I'm just starting to learn the Unity3D game development framework. I'm trying to make a cylinder "point" another object when some key is pressed.
public GameObject target;
void Update () {
if (Input.GetKeyDown(KeyCode.A)) {
???
}
}
I know that I have to use the target's and the cylinder's position to alter the cylinder's rotation, but I can't figure out how, I don't think I understand what those Quaternions are yet.
I'd really appreciate any help!
Thanks,
Manuel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您的圆柱体需要在圆柱体的局部空间中具有“向前”或其“指向”(我的话)的一些概念。为此,您可以假设(或直观地看到)+X、+Y、+Z、-X、-Y 或 -Z;或者您可以指定指向其他方向的任意向量。
其次,您需要一个从圆柱体中心指向另一个对象中心的向量(您已经提到过这一点)。
现在,您可以使用 Unity 的
Quaternion.FromToRotation(...)
生成一个四元数,如果将其应用于圆柱体的世界旋转,会将您的指向方向旋转到其他对象的方向。完毕。请注意,如果您的圆柱体深度超过几个变换,那么您可能需要稍微改变此方法的机制,以可能考虑父母的变换。
First, your cylinder needs some notion of 'forward' or its 'pointing direction' (my words) in the cylinder's local space. For this you can assume (or visually see) either +X, +Y, +Z, -X, -Y, or -Z; or you can specify your own arbitrary vector pointing in some other direction.
Second, you need to a vector that points from your cylinder's center to the other object's center (you mentioned this already).
Now, you can use Unity's
Quaternion.FromToRotation(...)
to generate a quaternion that, if applied to your cylinder's world rotation, will rotate your pointing direction to be in the direction of your other object. Done.Note that if your cylinder is more than a couple transforms deep, then you may need to alter the mechanics of this approach slightly to possibly account for parents' transforms.
您使用四元数有什么原因吗?我会使用 Quaternions.Eular 角度,它将四元数表示为向量 3,这就是我们通常理解角度的方式。
Ducky 所说的是正确的,但是如果您在使用四元数时遇到麻烦,我建议您在更好地理解这些角度集之前不要使用它们。
希望有帮助
Is there a reason as to why you are using Quaternions i would use Quaternions.Eular angles which represent Quaternions as a vector 3 which is how we commonly understand angles.
what Ducky said is correct however if you are having troubles with Quaternions i would recomend not using them until you have a better understanding of these angle sets.
hope it helps