- Home /
How to get a single Entity (or data for it) that a JobComponentSystem depends on for data?
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.
Answer by mentics · Jan 16, 2020 at 08:00 PM
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;
Your answer
Follow this Question
Related Questions
Label dots in between numbers (currency formatting) 2 Answers
How can I get a component data from an entity inside a JobComponentSystem? 1 Answer
Instantiated prefab missing ECS components 1 Answer
Will Allocator.Temp erase contents of my native containers? 1 Answer
Where is com.unity.animation? 1 Answer