简体   繁体   中英

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.

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. Can someone please help me ?

I tried this one but it is rotating the 2 objects in same axis

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:

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:

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 );    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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