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

Resolved Syncing Gameobject Rotation & Postion to Entity Without Weird Rotation

Discussion in 'Physics for ECS' started by LandonF, Jul 3, 2021.

  1. LandonF

    LandonF

    Joined:
    Mar 1, 2016
    Posts:
    34
    So I was using the built in script "Copy Transform to Game Object" to sync the game object from my converted entity to my entity's position / rotation. Everything works great until I edit the scale of my gameobject, then things get wonky. I recorded a video of the issue with one cube that's scale is = 1,1,1 and one with a scale of 2,2,2.



    The game object's position is okay, but the rotation is incorrect. I've found some solutions that involve simply tying the game object's position to the entity via a entitymanager.GetComponentData call every frame, but it's rather slow for many objects.

    The code I'm using is identical to unity's Entity Tracker. Which also produces the same results.
    Any help would be much appreciated.
    Thank you.
     
  2. LandonF

    LandonF

    Joined:
    Mar 1, 2016
    Posts:
    34
    Solved by creating my own syncing script & disabling "Jobs > Burst > Safety Checks"
    I'll leave the code here
    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3. using Unity.Jobs;
    4. using Unity.Burst;
    5. using Unity.Mathematics;
    6. using UnityEngine;
    7. using UnityEngine.Jobs;
    8.  
    9. namespace Unity.Transforms
    10. {
    11.     [UnityEngine.ExecuteAlways]
    12.     [UpdateInGroup(typeof(TransformSystemGroup))]
    13.     [UpdateAfter(typeof(EndFrameLocalToParentSystem))]
    14.     public class CopyTransformToGameObjectSystem : JobComponentSystem
    15.     {
    16.         [BurstCompile]
    17.         struct CopyTransforms : IJobParallelForTransform
    18.         {
    19.             [DeallocateOnJobCompletion]
    20.             [ReadOnly] public NativeArray<LocalToWorld> LocalToWorlds;
    21.  
    22.             public void Execute(int index, TransformAccess transform)
    23.             {
    24.                 var value = LocalToWorlds[index];
    25.                 transform.position = value.Position;
    26.                 transform.rotation = quaternion.LookRotationSafe(value.Forward, value.Up);
    27.             }
    28.         }
    29.  
    30.         EntityQuery m_TransformGroup;
    31.  
    32.         protected override void OnCreate()
    33.         {
    34.             m_TransformGroup = GetEntityQuery(ComponentType.ReadOnly(typeof(MyFixedSync)), ComponentType.ReadOnly<LocalToWorld>(), typeof(UnityEngine.Transform));
    35.         }
    36.  
    37.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    38.         {
    39.             var transforms = m_TransformGroup.GetTransformAccessArray();
    40.             var copyTransformsJob = new CopyTransforms
    41.             {
    42.                 LocalToWorlds = m_TransformGroup.ToComponentDataArrayAsync<LocalToWorld>(Allocator.TempJob, out inputDeps),
    43.             };
    44.  
    45.             return copyTransformsJob.Schedule(transforms, inputDeps);
    46.         }
    47.     }
    48. }
    49.  
    Big thanks to this very helpful thread https://forum.unity.com/threads/localtoworld-rotation-affected-by-entity-scale.882133/
     
    Last edited: Jul 9, 2021
    steveeHavok and JesOb like this.