Simple player not moving with pos.value

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

   public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
       
        MoveObjSpeed moveObj = new MoveObjSpeed{speed = 10};

        dstManager.AddComponentData(entity, moveObj);

       // PlayerTag myTag = new PlayerTag { };

        dstManager.AddComponent(entity, typeof(PlayerTag));

    }

after that, I created empty Player tag component Data and a move speed component data.

Now I added JobComponent like this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.Jobs;

public class MoveSYstem : JobComponentSystem
{
    [RequireComponentTag(typeof(PlayerTag))] //PlayerTag is empty
    struct MoveDataSystem : IJobForEach<Translation, Rotation, MoveObjSpeed>
    {

   
      

       
        public void Execute(ref Translation pos,ref Rotation rot, ref MoveObjSpeed speedObj)
        {

            Debug.Log(pos.Value); //this is showing player's initital value
            rot.Value = quaternion.Euler(10, 10, 10);
            pos.Value = new float3(3, 3, 3);
            Debug.Log(pos.Value); //it is showing updated value
         
        }
    }


    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var moveDatsaSystem = new MoveDataSystem
        {
           
        };
      //  Debug.Log("is it");
      


        return moveDatsaSystem.Schedule(this, inputDeps);

    }
}

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.

Because you should synch Entity position with GameObject position. In Convert add CopyTransformToGameObject component to your player entity.

1 Like

that worked, thanks man.

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

Remove GameObjectEntity from it. That is deprecated.

Can’t remove it, because “CopyTransformToGameovject…” needs it… https://prnt.sc/q7vmq5

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:

public class MyCopyTransformToGameObject : MonoBehaviour, IConvertGameObjectToEntity {
  public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) {
    dstManager.AddComponentData(entity, new Unity.Transforms.CopyTransformToGameObject());
  }
}
1 Like

I not told you add proxy, I told you add component directly in your Convert script )

1 Like

aah you guys saved my day, thanks so much!!!

I added this code as @GilCat shared:

    dstManager.AddComponentData(entity, new Unity.Transforms.CopyTransformToGameObject());

However, if I add

        dstManager.AddComponent(entity,typeof(Unity.Transforms.CopyTransformToGameObject));

Then also it works…

are there any benefits of using one over another?

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.

1 Like

make sense, thanks man