Search Unity

Robot - rotating child object with parent

Discussion in 'Physics' started by mark_de_jonge, Dec 7, 2019.

  1. mark_de_jonge

    mark_de_jonge

    Joined:
    Nov 11, 2019
    Posts:
    2
    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: https://github.com/qian256/ur5_unity. It currently uses slider values to rotate the joint instantly around z axis. This works fine.:


    The controller script uses this to rotate the axis:
    Code (CSharp):
    1. void LateUpdate () {
    2.         for ( int i = 0; i < 6; i ++) {
    3.             Vector3 currentRotation = jointList[i].transform.localEulerAngles;
    4.             Debug.Log(currentRotation);
    5.             currentRotation.z = jointValues[i];
    6.             jointList[i].transform.localEulerAngles = currentRotation;
    7.         }
    8.     }
    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:

    Code (CSharp):
    1.        joint0.rotation = Quaternion.RotateTowards(joint0.rotation, Quaternion.Euler(0, 0, joint0TargetRotation), rotSpeed * Time.deltaTime);
    2.         joint1.rotation = Quaternion.RotateTowards(joint1.rotation, Quaternion.Euler(0,0, joint1TargetRotation), rotSpeed * Time.deltaTime);
    3.         joint2.rotation = Quaternion.RotateTowards(joint2.rotation, Quaternion.Euler(0,0, joint2TargetRotation), rotSpeed * Time.deltaTime);
    4.         joint3.rotation = Quaternion.RotateTowards(joint3.rotation, Quaternion.Euler(0,0, joint3TargetRotation), rotSpeed * Time.deltaTime);
    5.         joint4.rotation = Quaternion.RotateTowards(joint4.rotation, Quaternion.Euler(0,0, joint4TargetRotation), rotSpeed * Time.deltaTime);
    6.         joint5.rotation = Quaternion.RotateTowards(joint5.rotation, Quaternion.Euler(0,0, joint5TargetRotation), rotSpeed * Time.deltaTime);
    Now the result looks like this:



    Any idea what I'm doing wrong? Cheers!
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    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).
     
    mark_de_jonge likes this.
  3. mark_de_jonge

    mark_de_jonge

    Joined:
    Nov 11, 2019
    Posts:
    2
    Thank you Edy, that totally fixed the problem! For any noob, like me, the solution was this:

    Code (CSharp):
    1.  joint0.localRotation = Quaternion.RotateTowards(joint0.localRotation, Quaternion.Euler(0, 90, joint0TargetRotation), rotSpeed * Time.deltaTime);
    2.         joint1.localRotation = Quaternion.RotateTowards(joint1.localRotation, Quaternion.Euler(0,0, joint1TargetRotation), rotSpeed * Time.deltaTime);
    3.         joint2.localRotation = Quaternion.RotateTowards(joint2.localRotation, Quaternion.Euler(0,0, joint2TargetRotation), rotSpeed * Time.deltaTime);
    4.         joint3.localRotation = Quaternion.RotateTowards(joint3.localRotation, Quaternion.Euler(0,0, joint3TargetRotation), rotSpeed * Time.deltaTime);
    5.         joint4.localRotation = Quaternion.RotateTowards(joint4.localRotation, Quaternion.Euler(0,90, joint4TargetRotation), rotSpeed * Time.deltaTime);
    6.         joint5.localRotation = Quaternion.RotateTowards(joint5.localRotation, Quaternion.Euler(0,0, joint5TargetRotation), rotSpeed * Time.deltaTime);
     
    Edy likes this.