Flo
Flo

Reputation: 97

Unity rotate object depending on other objects angle

I have two Gameobjects. The basic Question: "When I spin circle 1, I want to spin circle 2 in the same way manipulated by factor x"

How do I sync the rotation around each of their local axis of circle 2 with the interactable rotation of circle one and scale that rotation by factor x? Setting the transform.right equal doesnt work, there are still to many degrees of freedom. (Local Axis, because I want one or both gameobjects to be also tilted, but unrelated to one another.)

Trying math with rotational matrices didnt really work out based on the fact of them being evaluated every frame and thereby spinning Object 2 for eternity.

Thanks so much!

enter image description here

Upvotes: 3

Views: 1592

Answers (1)

derHugo
derHugo

Reputation: 90580

Assuming you rotate only around one single axis (as shown in the images) you can get the rotation difference in degrees of circle2 using e.g. Quaternion.Angle every frame

private Quaternion lastCircle2Rot;

//...

float rotDelta = Quaternion.Angle(lastCircle2Rot, circle2.transform.rotation);
lastCircle2Rot = circle2.transform.rotation;

than turn the circle1 accordignly using e.g. Transform.RotateAround

public float multiplier;

// e.g. rotate around local x = right
Vector3 YourAxis = circle1.transform.right;
circle1.transform.RotateAround(Vector3.zero, YourAxis, rotDelta * multiplier);

if using e.g. the right or another "clean" vector you could also simply use

circle1.transform.Rotate(Vector3.right, rotDelta * multiplier);

as it is done in local space by default.

Upvotes: 4

Related Questions