Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question How to rotate child entity in WorldSpace?

Discussion in 'Entity Component System' started by ATLGAN, Apr 23, 2024.

  1. ATLGAN

    ATLGAN

    Joined:
    Oct 15, 2017
    Posts:
    12
    I tried this code but not working.

    Code (CSharp):
    1. turretAspect.LocalTransform.ValueRW.Rotation = math.mul(math.inverse(playerAspect.LocalToWorld.ValueRO.Value),
    2.     quaternion.LookRotationSafe(dir, math.up()).value);
     

    Attached Files:

    • GIF.gif
      GIF.gif
      File size:
      7 MB
      Views:
      11
  2. ATLGAN

    ATLGAN

    Joined:
    Oct 15, 2017
    Posts:
    12
    I found this solution:
    Code (CSharp):
    1. float3 dir = float3.zero - turretAspect.LocalToWorld.ValueRO.Position;
    2. turretAspect.LocalTransform.ValueRW.Rotation = playerAspect.LocalToWorld.ValueRW.Value.InverseTransformRotation(quaternion.LookRotationSafe(dir, math.up())).value;
    This is work correctly but this time child enity shaking when rotating the parent entity. :(

    Also this is my move code of parent entity:

    Code (CSharp):
    1.     foreach (var playerAspect in SystemAPI.Query<PlayerAspect>())
    2.     {
    3.         playerAspect.PhysicsVelocity.ValueRW.Linear = PlayerCar.Instance.vehicleController.rb.velocity;
    4.         entityManager.SetComponentData(playerAspect.Entity, new LocalTransform
    5.         {
    6.             Position = playerAspect.Transform.ValueRO.Position,
    7.             Rotation = PlayerCar.Instance.vehicleController.transform.rotation,
    8.             Scale = playerAspect.Transform.ValueRO.Scale
    9.         });          
    10.     }
     

    Attached Files:

    Last edited: Apr 23, 2024