Robot - rotating child object with parent

I am trying to write a controller script that rotates each joint of a robot to a target position over one axis. The robot I’m using is coming from this Github page: GitHub - qian256/ur5_unity: A model and controller of UR5 (Universal Robots UR5) in Unity3D, supporting Augmented Reality (AR) and Virtual Reality (VR) applications.. It currently uses slider values to rotate the joint instantly around z axis. This works fine.:
shortlawfulhammerheadbird

The controller script uses this to rotate the axis:

void LateUpdate () {
        for ( int i = 0; i < 6; i ++) {
            Vector3 currentRotation = jointList[i].transform.localEulerAngles;
            Debug.Log(currentRotation);
            currentRotation.z = jointValues[i];
            jointList[i].transform.localEulerAngles = currentRotation;
        }
    }

The hierarchy looks like this:
https://imgur.com/a/54t4czH?third_party=1#_=_

Now I’m trying to write a controller script that takes in a float target z value and rotates the z axis towards this target position at a certain speed. I used this code:

       joint0.rotation = Quaternion.RotateTowards(joint0.rotation, Quaternion.Euler(0, 0, joint0TargetRotation), rotSpeed * Time.deltaTime);
        joint1.rotation = Quaternion.RotateTowards(joint1.rotation, Quaternion.Euler(0,0, joint1TargetRotation), rotSpeed * Time.deltaTime);
        joint2.rotation = Quaternion.RotateTowards(joint2.rotation, Quaternion.Euler(0,0, joint2TargetRotation), rotSpeed * Time.deltaTime);
        joint3.rotation = Quaternion.RotateTowards(joint3.rotation, Quaternion.Euler(0,0, joint3TargetRotation), rotSpeed * Time.deltaTime);
        joint4.rotation = Quaternion.RotateTowards(joint4.rotation, Quaternion.Euler(0,0, joint4TargetRotation), rotSpeed * Time.deltaTime);
        joint5.rotation = Quaternion.RotateTowards(joint5.rotation, Quaternion.Euler(0,0, joint5TargetRotation), rotSpeed * Time.deltaTime);

Now the result looks like this:

distantdeafeningaustraliankelpie

Any idea what I’m doing wrong? Cheers!

At first sight, it looks like the second example is using world-rotations (Transform.rotation), while the first one is using local rotations (Transform.localEulerAngles). You may try using local rotations in the second example (Transform.localRotation).

1 Like

Thank you Edy, that totally fixed the problem! For any noob, like me, the solution was this:

 joint0.localRotation = Quaternion.RotateTowards(joint0.localRotation, Quaternion.Euler(0, 90, joint0TargetRotation), rotSpeed * Time.deltaTime);
        joint1.localRotation = Quaternion.RotateTowards(joint1.localRotation, Quaternion.Euler(0,0, joint1TargetRotation), rotSpeed * Time.deltaTime);
        joint2.localRotation = Quaternion.RotateTowards(joint2.localRotation, Quaternion.Euler(0,0, joint2TargetRotation), rotSpeed * Time.deltaTime);
        joint3.localRotation = Quaternion.RotateTowards(joint3.localRotation, Quaternion.Euler(0,0, joint3TargetRotation), rotSpeed * Time.deltaTime);
        joint4.localRotation = Quaternion.RotateTowards(joint4.localRotation, Quaternion.Euler(0,90, joint4TargetRotation), rotSpeed * Time.deltaTime);
        joint5.localRotation = Quaternion.RotateTowards(joint5.localRotation, Quaternion.Euler(0,0, joint5TargetRotation), rotSpeed * Time.deltaTime);
1 Like