Search Unity

Resolved Problems with interaction with GameObject from ECS

Discussion in 'Entity Component System' started by FederalRazer89, Mar 22, 2023.

  1. FederalRazer89

    FederalRazer89

    Joined:
    Nov 24, 2019
    Posts:
    83
    Version:
    Unity: 2022.2.5f
    Burst: 1.8.2
    Entities: 1.0.0-pre.15

    Hello

    This might be a something small that i am missing at the moment, but i cant interact with the GameObject. In 0.17 i was able to make it work.

    I can also se that the systemBase that i want to use for the gameobject dont print in the debug.
    I dont get any errors or nothing so i am a bit confused, any hints or help would be nice.

    Code (CSharp):
    1.  
    2. public struct CharacterControllerData : IComponentData
    3. {
    4.  
    5.     public float velocityX;
    6.     public float velocityY;
    7.  
    8. }
    9.  
    10. public class CharacterControllerClass : IComponentData
    11. {
    12.     public GameObject characterControllerGO;
    13.     public Animator animator;
    14.  
    15. }
    16.  


    Code (CSharp):
    1.  
    2. public partial class CharacterControllerGeneral : SystemBase
    3. {
    4.  
    5.  
    6.     protected override void OnUpdate()
    7.     {
    8.         foreach (RefRW<CharacterControllerData> characterControllerData in SystemAPI.Query<RefRW< CharacterControllerData >>())
    9.         {
    10.             characterControllerData.ValueRW.velocityX = characterControllerData.ValueRO.velocityX + 0.1f;
    11.             characterControllerData.ValueRW.velocityY = characterControllerData.ValueRO.velocityY + 0.1f;
    12.  
    13.             if ((characterControllerData.ValueRO.velocityX > 0.9f) && (characterControllerData.ValueRO.velocityY > 0.9f))
    14.             {
    15.                 characterControllerData.ValueRW.velocityX = -1;
    16.                 characterControllerData.ValueRW.velocityY = -1;
    17.             }
    18.  
    19.             Debug.Log("Woring ");
    20.  
    21.         }
    22.  
    23.         foreach ((RefRO<CharacterControllerData> characterControllerData, CharacterControllerClass CharacterControllerClass) in SystemAPI.Query<RefRO<CharacterControllerData>,CharacterControllerClass>())
    24.         {
    25.             CharacterControllerClass.animator.SetFloat("Velocity X", characterControllerData.ValueRO.velocityX);
    26.             CharacterControllerClass.animator.SetFloat("Velocity Y", characterControllerData.ValueRO.velocityY);
    27.             Debug.Log("Woring ");
    28.  
    29.         }
    30.  
    31.     }
    32.  
    33.     //(var(qd, localToWorld) in SystemAPI.Query<QueryData, RefRO<LocalToWorld>>())
    34. }
    35.  

    Code (CSharp):
    1.  
    2. public class CharacterControllerAuthoring : MonoBehaviour
    3. {
    4.     public GameObject characterControllerGO;
    5.     public Animator animator;
    6.  
    7.     public float velocityX = 0;
    8.     public float velocityY = 0;
    9.  
    10.  
    11.  
    12.     //void OnEnable() { }
    13. }
    14.  
    15. public class CharacterBaker : Baker<CharacterControllerAuthoring>
    16. {
    17.     public override void Bake(CharacterControllerAuthoring authoring)
    18.     {
    19.         //var transform = GetComponent<Transform>(authoring.characterControllerGO);
    20.  
    21.         DependsOn(authoring.animator);
    22.         AddComponent(new CharacterControllerData
    23.         {
    24.             velocityX = authoring.velocityX,
    25.             velocityY = authoring.velocityY
    26.  
    27.         });
    28.  
    29.         AddComponentObject(
    30.         new CharacterControllerClass
    31.         {
    32.             characterControllerGO = authoring.characterControllerGO,
    33.             animator = authoring.animator
    34.  
    35.         });
    36.  
    37.     }
    38.  
    39.  
    40. }
    41.  

    upload_2023-3-22_5-6-0.png
    upload_2023-3-22_5-6-14.png

    upload_2023-3-22_5-6-25.png
     
  2. FederalRazer89

    FederalRazer89

    Joined:
    Nov 24, 2019
    Posts:
    83
    I think i uploaded this question a bit hasty, i just missunderstod some of the new syntaxes with ECS 10. vs ECS 0.17.

    Well here is my current temporary solution for sending data from ECS to GameObject. Will check out DOTS animation when i got some other stuff done, this will have to do.

    A spawner object in a subscene.
    upload_2023-3-24_16-3-17.png

    Code (CSharp):
    1.  
    2. public class ECSSpawnHybridAuthoring : MonoBehaviour
    3. {
    4.     public GameObject refGameObject;
    5.  
    6. }
    7.  
    8.  
    9. public class ECSSpawnHybridBaker : Baker<ECSSpawnHybridAuthoring>
    10. {
    11.     public override void Bake(ECSSpawnHybridAuthoring authoring)
    12.     {
    13.  
    14.         AddComponentObject(new ECSSpawnHybrid
    15.         {
    16.             gameObjectInstance = authoring.refGameObject
    17.  
    18.         });
    19.  
    20.     }
    21. }
    22.  

    Code (CSharp):
    1.  
    2.  
    3. public class ECSSpawnHybrid : IComponentData
    4. {
    5.     public GameObject gameObjectInstance;
    6.  
    7. }
    8.  
    9. public partial class ECSSpawnHybridSystem : SystemBase
    10. {
    11.     public bool spawnCharacter;
    12.     protected override void OnCreate()
    13.     {
    14.     }
    15.  
    16.     protected override void OnStartRunning()
    17.     {
    18.         spawnCharacter = true;
    19.     }
    20.  
    21.     protected override void OnDestroy()
    22.     {
    23.     }
    24.  
    25.     protected override void OnUpdate()
    26.     {
    27.         var ecb = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(World.Unmanaged);
    28.  
    29.         Entities.ForEach
    30.             ((ECSSpawnHybrid spawner) =>
    31.             {
    32.                 if (spawnCharacter == true)
    33.                 {
    34.                     GameObject instanceGO = MonoBehaviour.Instantiate(spawner.gameObjectInstance);
    35.                     Entity newEntity = ecb.CreateEntity();
    36.                     newEntity = ecb.Instantiate(newEntity);
    37.  
    38.                     ecb.AddComponent<CharacterControllerData>(newEntity,
    39.                         new CharacterControllerData
    40.                         {
    41.                             position = new float3(instanceGO.transform.position),
    42.                             velocityX = 0,
    43.                             velocityY = 0
    44.                      
    45.                         });
    46.  
    47.  
    48.                     ecb.AddComponent<CharacterControllerClass>(newEntity,
    49.                         new CharacterControllerClass
    50.                         {
    51.                             characterControllerGO = instanceGO,
    52.                             animator = instanceGO.GetComponent<Animator>()
    53.                         });
    54.  
    55.  
    56.                     spawnCharacter = false;
    57.                     Debug.Log("Spawn ");
    58.                 }
    59.  
    60.             }
    61.             ).WithoutBurst().Run();
    62.  
    63.     }
    64.  
    65. }
    66.  
    67.  
    68.  

    Code (CSharp):
    1.  
    2. public partial class CharacterControllerGeneral : SystemBase
    3. {
    4.     public bool spawnCharacter;
    5.  
    6.  
    7.     protected override void OnCreate()
    8.     {
    9.     }
    10.  
    11.     protected override void OnDestroy()
    12.     {
    13.     }
    14.  
    15.     protected override void OnUpdate()
    16.     {
    17.         float deltaTime = SystemAPI.Time.DeltaTime;
    18.  
    19.         //Controlling values for animator
    20.         Entities.ForEach
    21.             ((ref CharacterControllerData characterControllerData) =>
    22.                 {
    23.                     characterControllerData.velocityX = characterControllerData.velocityX + (0.1f * deltaTime);
    24.                     characterControllerData.velocityY = characterControllerData.velocityY + (0.1f * deltaTime);
    25.  
    26.                     if ((characterControllerData.velocityX > 0.9f) && (characterControllerData.velocityY > 0.9f))
    27.                     {
    28.                         characterControllerData.velocityX = -1;
    29.                         characterControllerData.velocityY = -1;
    30.                     }
    31.  
    32.                 }
    33.             ).WithBurst().Schedule();
    34.  
    35.         //Controlling position of Character
    36.         Entities.ForEach
    37.             ((ref CharacterControllerData characterControllerData) =>
    38.             {
    39.  
    40.                 characterControllerData.position = new float3((1f * deltaTime), 0, (1f * deltaTime)) + characterControllerData.position;
    41.  
    42.                 if ((characterControllerData.position.x > 10) && (characterControllerData.position.z > 10))
    43.                 {
    44.                     characterControllerData.position = new float3(-10,0,-10);
    45.                 }
    46.  
    47.  
    48.             }
    49.             ).WithBurst().Schedule();
    50.  
    51.         //Reading from CharacterControllerData and setting values for GameObject and Animator
    52.         Entities.ForEach
    53.         ((CharacterControllerClass characterControllerClass, in CharacterControllerData characterControllerData ) =>
    54.         {
    55.             characterControllerClass.animator.SetFloat("VelocityX", characterControllerData.velocityX);
    56.             characterControllerClass.animator.SetFloat("VelocityY", characterControllerData.velocityY);
    57.  
    58.             characterControllerClass.characterControllerGO.transform.position = characterControllerData.position;
    59.  
    60.             Debug.Log("CharacterControllerClass.animator " + characterControllerClass.animator.GetFloat("VelocityX") + " : " + characterControllerClass.animator.GetFloat("VelocityY") + "   Pos " + characterControllerClass.characterControllerGO.transform.position);
    61.  
    62.         }
    63.         ).WithoutBurst().Run();
    64.  
    65.  
    66.     }
    67.  
    68. }
    69.  

    Code (CSharp):
    1.  
    2. public struct CharacterControllerData : IComponentData
    3. {
    4.     public float3 position;
    5.  
    6.     public float velocityX;
    7.     public float velocityY;
    8.  
    9. }
    10.  
    11. public class CharacterControllerClass : IComponentData
    12. {
    13.     public GameObject characterControllerGO;
    14.     public Animator animator;
    15.  
    16. }
    17.