I've got an application where I'm trying to display an object in 3D, with information provided by real-world accelerometers and inclinometers - sensors that pick up the angle of the equipment, relative to ground. The trouble comes when I want to show this 'world pitch' or 'world roll' on an object that is a child of another. I'd think that there is a trivial solution to the problem, but for the life of me, I can't figure out what it is.

Let's say I've got a robot arm with several pieces - "LowerArm", "Elbow", "UpperArm" and "Hand". "Hand" is the child of the "UpperArm", and "UpperArm" is the child of "Elbow". I'd like to place "Hand" at 0 degree pitch, relative to world-space, no matter how I move the parent objects. If I grab my "LowerArm", and rotate it 90 degrees, the hand should still lay 'flat', relative to the ground; in local coordinate space, it should moved 90 degrees, to compensate.

I want it to remain attached; so it should continue to follow the other parts, in terms of roll and yaw. If I twist the arm, the hand twists with everything else - it just maintains that 0 degree world pitch. Observe the yellow object, in the below example:

enter image description here

How can I do this? I would have thought that there was some kind of trick, with matrices or quaternions, that could resolve it; but I've had no luck, so far. It's hard to figure out what the new 'forward' vector should be.

  • \$\begingroup\$ Anyone got any ideas? I'm tearing my hair out here, and I don't have all that much left! \$\endgroup\$ Jun 7, 2017 at 12:43

1 Answer 1


A user over at UnityAnswers gave me the solution to the problem.

Assuming you've got a variable named 'worldPitch', you can do the following (Unity C# code), although ...

Vector3 forward = Vector3.Cross(transform.parent.right, Vector3.up);
Vector3 up = Vector3.Cross(forward, transform.parent.right);
transform.rotation = Quaternion.LookRotation(forward, up) * Quaternion.Euler(new Vector3(worldPitch, 0, 0));

You can extend this to other orientations (ie. roll, pitch or yaw) by adjusting the axes.



You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .