Search Unity

Simple player not moving with pos.value

Discussion in 'Entity Component System' started by idurvesh, Dec 8, 2019.

  1. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    I am trying out ECS.

    I added a new cube, named it a "Player".

    I added "Convert to Entity" script on a Player with conversion as "Convert And Inject Game Object"

    Then added a monobheaviour with IConvertGameObjectToEntity

    Code (CSharp):
    1.    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    2.     {
    3.        
    4.         MoveObjSpeed moveObj = new MoveObjSpeed{speed = 10};
    5.  
    6.         dstManager.AddComponentData(entity, moveObj);
    7.  
    8.        // PlayerTag myTag = new PlayerTag { };
    9.  
    10.         dstManager.AddComponent(entity, typeof(PlayerTag));
    11.  
    12.     }
    after that, I created empty Player tag component Data and a move speed component data.

    Now I added JobComponent like this

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Entities;
    5. using Unity.Transforms;
    6. using Unity.Mathematics;
    7. using Unity.Jobs;
    8.  
    9. public class MoveSYstem : JobComponentSystem
    10. {
    11.     [RequireComponentTag(typeof(PlayerTag))] //PlayerTag is empty
    12.     struct MoveDataSystem : IJobForEach<Translation, Rotation, MoveObjSpeed>
    13.     {
    14.  
    15.    
    16.      
    17.  
    18.        
    19.         public void Execute(ref Translation pos,ref Rotation rot, ref MoveObjSpeed speedObj)
    20.         {
    21.  
    22.             Debug.Log(pos.Value); //this is showing player's initital value
    23.             rot.Value = quaternion.Euler(10, 10, 10);
    24.             pos.Value = new float3(3, 3, 3);
    25.             Debug.Log(pos.Value); //it is showing updated value
    26.          
    27.         }
    28.     }
    29.  
    30.  
    31.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    32.     {
    33.         var moveDatsaSystem = new MoveDataSystem
    34.         {
    35.            
    36.         };
    37.       //  Debug.Log("is it");
    38.      
    39.  
    40.  
    41.         return moveDatsaSystem.Schedule(this, inputDeps);
    42.  
    43.     }
    44. }
    45.  
    I get the two Debug.Log with the player's previous and new position. But in the editor, I can't see the player changing its position.

    What's wrong with my code? Kindly help.
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Because you should synch Entity position with GameObject position. In Convert add CopyTransformToGameObject component to your player entity.
     
    idurvesh likes this.
  3. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    that worked, thanks man.

    BUt I am getting this Red warning when I add the script.
    https://prnt.sc/q7v7mb
     
  4. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Remove GameObjectEntity from it. That is deprecated.
     
  5. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    Can't remove it, because "CopyTransformToGameovject..." needs it... https://prnt.sc/q7vmq5
     
  6. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Yeah you are right, i was forgetting that ComponentDataProxy<> enforces GameObjectEntity.
    Then you either leave it like that and ignore the message, but bear in mind that it will be deprecated and removed soon or later, or just create a component to convert it like this:
    Code (CSharp):
    1.  
    2. public class MyCopyTransformToGameObject : MonoBehaviour, IConvertGameObjectToEntity {
    3.   public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) {
    4.     dstManager.AddComponentData(entity, new Unity.Transforms.CopyTransformToGameObject());
    5.   }
    6. }
     
    idurvesh likes this.
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    I not told you add proxy, I told you add component directly in your Convert script )
     
    idurvesh likes this.
  8. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    aah you guys saved my day, thanks so much!!!

    I added this code as @GilCat shared:
    Code (CSharp):
    1.     dstManager.AddComponentData(entity, new Unity.Transforms.CopyTransformToGameObject());
    However, if I add
    Code (CSharp):
    1.         dstManager.AddComponent(entity,typeof(Unity.Transforms.CopyTransformToGameObject));
    Then also it works..

    are there any benefits of using one over another?
     
  9. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    Functionally there's no difference for tag components. One code path might be faster than the other, but it is difficult to tell which from looking at the internals. You'd have to profile and see.

    For non-tag components, the first method lets you initialize values while the second just adds a default of the type but lets you work with types at runtime.
     
    idurvesh likes this.
  10. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    make sense, thanks man