Link object rotations per axis without parenting

Hi guys, I’ve searched for days but haven’t quite found the answer yet; sorry if it’s already out there.

I’m trying to make a script that will behave like the wire parameters in 3D Max. For those unfamiliar with wire parameters, it lets you link Obj1’s X Rotation to Obj2’s Y Position for example. This allows me to rotate Obj1 to raise or lower Obj2. You can also link position to position and rotation to rotation.

That being said, matching position to position per axis is fairly simple. Where I’m having trouble is with the rotations; specifically the X rotation. Say I have a MasterObj and a SlaveObj. If I link the X Rotations, when I rotate the MasterObj, the SlaveObj ping-pongs between -90 and 90 degrees, boosting the Y and Z axes to 180. No matter if I use Euler angles or Quaternions.

``````if (followRotationX == true){
rotX = target.transform.localEulerAngles.x;
}else{
rotX = transform.localEulerAngles.x;
}

if (followRotationY == true){
rotY = target.transform.localEulerAngles.y;
}else{
rotY = transform.localEulerAngles.y;
}

if (followRotationZ == true){
rotZ = target.transform.localEulerAngles.z;
}else{
rotZ = transform.localEulerAngles.z;
}

Vector3 rot = new Vector3(rotX,rotY,rotZ);
transform.localEulerAngles = rot;
``````

For now, all I want is for my SlaveObj to behave like if it was parented to my MasterObj, all while having control over whether I want it to follow the position and rotation and on which axes.

Basically, this : http://constraints.path-o-logical.com/ but allowing me to link rotations and position, cross-axis (RotY to RotZ, PosX to PosZ, etc.)

Anyone have any wise advice? Thanks !

P.S. I’m a 3D artist who dabbles in code sometimes…

Your issue is with reading ‘eulerAngles’. Internally Unity stores rotations as Quaternions, so when you read back eulerAngles, it is derived from the Quaternion. But the angle you put in will not necessary be the angle you get back out (though it will be the same physical rotation). For example put this in a script:

``````transform.eulerAngles = Vector3(180,0,0);
Debug.Log(transform.eulerAngles);
``````

The output will be (0,180,180)…the same physical rotation represented differently.

The solution will depend on how you are doing your rotation. Often you can treat eulerAngles as ‘write-only’. Maintain your own Vector3, modify and then assign the Vector3 to transform.eulerAngles to rotate. Since you are the one modifying/maintaining the Vector3, the rotations you read from the Vector3 will always be in the representation you desire.

There are other approaches if maintaining your own Vector3 does not work.