Question by
nitsnbolts_unity · Dec 10, 2020 at 07:33 AM ·
c#rotationquaternionaxiseulerangles
Quaternions: Setting rotation of one gameObject to Y,Z-Axis rotation of gameObject-A and Z-Axis position of gameObject-B
I'm trying to set the rotation of a gameObject so that it's X,Y,Z rotation is based on 2 seperate gameObjects:
The Y and Z-Axis rotation is based on Y and Z-axis rotation of one gameObject.
And its X-Axis rotation is based on the Z-Axis localPosition of another gameObject.
Any idea on how to go about doing this?
What I have so far:
Quaternion combinedQuaternion;
GameObject mainGameObject;
GameObject rotateAxisYZ;
GameObject moveAxisZ;
float xRotate, yRotate, zRotate;
void RotateMainObject()
{
// Setting x, y, z euler angle values of mainGameObject to rotateAxisYZ and moveAxisZ
xRotate = moveAxisZ.transform.localPosition.z * 20;
yRotate = rotateAxisYZ.transform.localEulerAngles.y;
zRotate = rotateAxisYZ.transform.localEulerAngles.z;
mainGameObject.transform.localEulerAngles = new Vector3(xRotate, yRotate, zRotate);
// Combining euler angles into Quaternion form and setting it to mainGameObject rotation
combinedQuaternion.eulerAngles = mainGameObject.transform.localEulerAngles;
mainGameObject.transform.rotation = combinedQuaternion;
}
This works well up to a point it seems. As long as the main gameObject is rotating on the X and Y axis alone - all's good. Once I introduce Z axis rotation - there's axis swapping and everything goes haywire. Anyone know of any possible solutions?
Comment