简体   繁体   English

如何将目标对象围绕轴 A 旋转不同的对象围绕轴 B 旋转多少?

[英]How do I rotate a target object around axis A by how much a different object is rotated around axis B?

I have a steering wheel that I want to rotate in Y axis.我有一个要在 Y 轴上旋转的方向盘。

By rotating this wheel I want to rotate a mirror and this mirror to follow the rotation of steering wheel, but in Z axis and not in Y axis.通过旋转这个轮子,我想旋转一面镜子,这个镜子跟随方向盘的旋转,但在 Z 轴而不是 Y 轴上。 Can someone please help me ?有人可以帮帮我吗 ?

I tried this one but it is rotating the 2 objects in same axis我试过这个,但它在同一轴上旋转 2 个对象

void Update()
{
    mirror.transform.localRotation = stairing.transform.localRotation;
}

You can get the steering wheel's y rotation with Quaternion.eulerAngles and getting the y component:您可以使用Quaternion.eulerAngles获取方向盘的y旋转并获取y分量:

float rotateAngle = stairing.transform.localRotation.eulerAngles.y; 

You can set the mirror's localRotation to only rotate by rotateAngle around the Z axis by calling Quaternion.Euler with a forward vector whose length is the angle you'd like to rotate:您可以通过调用Quaternion.Euler并使用前向向量(其长度是您要旋转的角度)将镜子的localRotation设置为仅通过rotateAngle围绕 Z 轴旋转:

mirror.transform.localRotation = Quaternion.Euler(Vector3.forward * rotateAngle);

In your case, combining them might look like this:在您的情况下,将它们组合起来可能如下所示:

void Update()
{
    float rotateAngle = stairing.transform.localRotation.eulerAngles.y;
    mirror.transform.localRotation = Quaternion.Euler(Vector3.forward * rotateAngle );    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM