Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Transform.Rotation to Entity.LocalTransform.Rotation

Discussion in 'Entity Component System' started by andreyakladov, Apr 2, 2023.

  1. andreyakladov

    andreyakladov

    Joined:
    Nov 11, 2016
    Posts:
    29
    A dummy question which I clearly can't get right: how one should transfer GameObject's beautiful Transform.Rotation to Entity.LocalTransform.Rotation in code?

    I tried:
    Code (CSharp):
    1.  
    2. new LocalTransform
    3. {
    4.    Position = prefab.placementInfo.position,
    5.    Rotation = quaternion.Euler(
    6.             math.radians(prefab.placementInfo.rotation.x),
    7.             math.radians(prefab.placementInfo.rotation.y),
    8.             math.radians(prefab.placementInfo.position.z),
    9.             math.RotationOrder.XYZ), // tried different RotationOrder. and it did not work
    10.    Scale = prefab.placementInfo.scale
    11. }
    Could anyone please help?
     
  2. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    177
    Transform.Rotation is quaternion. LocalTransform.Rotation is also quaternion. So, given that there is implicit conversion from UnityEngine.Quaternion to Unity.Mathematics.quaternion the answer is simple assignment:
    Code (CSharp):
    1.  
    2. new LocalTransform
    3. {
    4.     Position = prefab.placementInfo.position,
    5.     Rotation = prefab.placementInfo.rotation,
    6.     Scale = prefab.placementInfo.localScale.x,
    7. }
    8.  
    But I belive that you have something other than UnityEngine.Transform, because there is no 'scale' field in it.
     
  3. andreyakladov

    andreyakladov

    Joined:
    Nov 11, 2016
    Posts:
    29
    ah, yes, but PlacementInfo is a custom structure and Rotation field is actually a Vector3 with 3 angles.
     
  4. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    177
    Default rotation order when converting form eulers to quaternion is ZXY. So here is conversion:

    var rot = quaternion.EulerZXY(math.radians(prefab.placementInfo.rotation.xyz));


    But if you need to transfer rotation from UnityEngine.Transform you should not do this double conversion (quaternion->euler->quaternion)