Search Unity

bone.localEulerAngles doesn't give me correct result

Discussion in 'Scripting' started by dhanyfling, Nov 20, 2020.

  1. dhanyfling

    dhanyfling

    Joined:
    Jul 25, 2019
    Posts:
    3
    i use a model from Make Human so the bones created by Make Human software by default
    i want to manipulate JAW bone using Euler. in the end of animation i want this JAW bone come back to initial position. this is my simplified codes

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class MyTestBone : MonoBehaviour {
    7.  
    8.     Transform bone;
    9.     float initialJaw_x_Angle;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         bone = GetComponent<Transform>().Find("MakeHuman default skeleton/root/spine05/spine04/spine03/spine02/spine01/neck01/neck02/neck03/head/jaw");
    14.         initialJaw_x_Angle = bone.localEulerAngles.x;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.      
    20.     }
    21.  
    22.     void LateUpdate(){
    23.         bone.localEulerAngles = new Vector3 (initialJaw_x_Angle, 0, 0);
    24.     }
    25. }
    26.  
    in my logic
    initialJaw_x_Angle 
    will gives me angle value before Jaw bone manipulated. but the result is different. the Jaw bone doesn't come back to initial angle.
    Thanks for your help
     
  2. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    The XYZ components of Euler angles are close to meaningless when split apart, and generally should just never be split apart (which is what you're doing on line 14). The main reason is because you can't guarantee that the Y and Z components will be any particular value - and the Inspector is not your friend here. The Inspector may show you 45,0,0 while .localEulerAngles returns 135,180,180, or something like that. These are both valid Euler angles for the underlying Quaternion rotation, and the Transform class importantly has no idea which of these are typed into the Inspector.

    A better approach is to store the entire starting Quaternion (not just the X component), and then set the rotation with something like:
    Code (csharp):
    1. bone.localRotation = startingRotation * Quaternion.Euler(xRotOffset, 0f, 0f);
     
    dhanyfling likes this.
  3. dhanyfling

    dhanyfling

    Joined:
    Jul 25, 2019
    Posts:
    3
    thanks for your help,
    what is xRotOffset ?,

    I followed your suggestion by using Quartenion and it works, this is my solution
    Code (CSharp):
    1.  
    2.  
    3. //this is my test to manipulate bone
    4. //i want the manipated bone goes back to initial rotation after so many manipulation
    5.  
    6. using System.Collections;
    7. using System.Collections.Generic;
    8. using UnityEngine;
    9.  
    10. public class MyTestBone : MonoBehaviour {
    11.    
    12.     Transform bone;
    13.     Quaternion initialRotation;  
    14.    
    15.    
    16.     // Use this for initialization
    17.     void Start () {
    18.         bone = GetComponent<Transform>().Find("MakeHuman default skeleton/root/spine05/spine04/spine03/spine02/spine01/neck01/neck02/neck03/head/jaw");
    19.         initialRotation = bone.rotation;
    20.         bone.localEulerAngles = new Vector3 (152.714f + 45, 0, 0);
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.        
    26.     }
    27.    
    28.     void LateUpdate(){
    29.        
    30.         //bone.rotation = initialRotation;     //this work but move instantly not animated
    31.        
    32.         var step = 90 * Time.deltaTime; // The step size is equal to speed times frame time.
    33.  
    34.         // Rotate our transform a step closer to the target's.
    35.         bone.rotation = Quaternion.RotateTowards(bone.rotation, initialRotation, step); //it's work and animated well
    36.     }
    37. }
    38.  
    btw.., thanks for your suggestion
     
    Last edited: Nov 21, 2020