Let’s say I have many enemy Entity
s that are going to run away from a player Entity
. In the JobComponentSystem
for those objects, how would I get the position of the player so they can run away? I would use the Entities.ForEach on an Enemy : IComponentData
, but I need the player position to know which direction to run.
One way to do it is to use an EntityQuery
to get it in advance. It seems to work if you put this in the OnStartRunning
method in your ComponentSystem
:
PlayerDataOrTag player;
protected override void OnStartRunning() {
var playerEntity = GetEntityQuery(typeof(PlayerDataOrTag)).GetSingletonEntity();
player = EntityManager.GetComponentData<PlayerDataOrTag>(playerEntity);
}
and then you can use a local variable in OnUpdate:
float3 playerPos = EntityManager.GetComponentData<Translation>(player).Value;