Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

TransformDirection and eulerAngles

Discussion in 'Documentation' started by LokenGarviel, Aug 27, 2020.

?

how are TransformDirection and eulerAngles related?

  1. no related

    0 vote(s)
    0.0%
  2. because the translation is based on the angle

    2 vote(s)
    100.0%
  1. LokenGarviel

    LokenGarviel

    Joined:
    Aug 25, 2020
    Posts:
    12
    I wonder how TransformDirection works in the documentation it says that it performs the transformation from local to world, but in the script to rotate the character towards the camera is used:
    ```
    target.eulerAngles = new Vector3(0, target.eulerAngles.y, 0);
    movement = target.TransformDirection(movement);
    ```
    I don’t understand how the eulerAngles change works here, it doesn’t allow rotation in other directions, but why? TransformDirection does just translation. Why is the axis locking done this way?
     

    Attached Files:

  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    To rotate the character towards the camera:
    Code (CSharp):
    1. // look at a transform:
    2. target.LookAt(Camera.main.transform);
    3. // or look a position (world space):
    4. target.LookAt(Camera.main.transform.position);
    Then after you could just do this to only have the Y rotation:
    target.rotation = Quaternion.Euler(target.rotation.eulerAngles * Vector3.up);

    Edit: actually just checked the line above, won't allow multiplying Vector3s, so could do this instead:
    Code (CSharp):
    1. Vector3 r = target.rotation.eulerAngles;
    2. r.x = r.z = 0;
    3. target.rotation = Quaternion.Euler(r);
     
  3. LokenGarviel

    LokenGarviel

    Joined:
    Aug 25, 2020
    Posts:
    12
    yes, I understand that you can do the turn in a different way, but I'm just wondering how are eulerAngles and TransformDirection related? That is, does TransformDirection use eulerAngles, because it turns out that yes, and nowhere is this written or I just can't find
     
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    I'm pretty sure that target.rotation.eulerAngles would be the same value as target.TransformDirection(target.localRotation.eulerAngles), and target.localRotation.eulerAngles would be the same as target.InverseTransformDirection(target.rotation.eulerAngles). I'm not sure if that answers what you're asking (but I'm pretty sure that's how it is).
     
  5. LokenGarviel

    LokenGarviel

    Joined:
    Aug 25, 2020
    Posts:
    12
    thanks for the help anyway
     
    adamgolden likes this.