But it seems like the followers lag behind every time the target moves, especially the 2nd and 3rd follower.
I am not sure what went wrong.
Any help would be great, thanks!
This code is actually a port from the ECS version. It seems to be working well in ECS without any lagging behind issues. So I am confused what went wrong.
[BurstCompile]
public partial struct FollowAtDistanceJob : IJobEntity
{
public TransformAspect.Lookup AllTransform;
public EntityStorageInfoLookup EntityStorageInfoLookup;
void Execute(Entity e, in FollowAtDistanceData followTarget)
{
bool hasNoTarget = !EntityStorageInfoLookup.Exists(followTarget.Target);
if (hasNoTarget)
{
return;
}
TransformAspect transform = AllTransform[e];
TransformAspect targetTransform = AllTransform[followTarget.Target];
float3 targetPosition = targetTransform.Position;
float2 transformPosition2D = transform.Position.xz;
float2 targetPosition2D = targetPosition.xz;
float distanceToTarget = math.distance(transformPosition2D, targetPosition2D);
bool isTooFar = distanceToTarget > followTarget.Distance;
if (isTooFar)
{
float3 direction = new float3(
targetPosition2D.x,
0,
targetPosition2D.y
);
float3 delta = Float2Extensions.MoveTowardsDelta(
transformPosition2D,
targetPosition2D,
distanceToTarget - followTarget.Distance
).ToX0Y();
transform.TranslateWorld(delta);
transform.LookAt(direction);
}
}
}
If your ECS version works and the Job one doesn’t, maybe the problem is not in the job itself, but rather in how you are scheduling it. Maybe the input data is not being updated correctly into newly scheduled jobs or something.
I’ve decided not to use job for this specific case since I am in a deadline, and have not pursued it further since then since its working fine without any performance issues. I’ll re-investigate when I will need to come back to it.