Recently learned about this ECS/DOTS method in Unity and was incredibly impressed with how fast it was. Definitely wanting to try it out, but I have a good few problems due to not understanding how to get a list sorted during a job (that, and parenting). I’m extremely new to this, and have watched a few tutorials, but they didn’t really show about getting a sorted list of targets. The closest was CodeMonkey’s, which did help to get the closest target, but only the closest target.
This is what I had in Monobehaviour. It basically would find a list of targets within a certain distance from the mouse, then sort them by distance from mouse. Said sorted list would then be used for a homing missile attack, where 8 missiles would be fired and each missile headed for each of the first 8 targets in the list.
public class GunFunctionHelper: MonoBehaviour // manages firing intervals and targeting
{
public List<EntityScript> targets = new List<EntityScript>();
//...
//...
//...
public void getTargetsAroundCursor(List<EntityScript> allTargetsList)
{
Vector3 v3 = Input.mousePosition;
float cursorRadius = 5;
float camX = Camera.main.transform.position.x;
float camY = Camera.main.transform.position.y;
float camZ = Camera.main.transform.position.z;
v3.z = -Camera.main.transform.position.z;
v3 = Camera.main.ScreenToWorldPoint(v3);
targets.Clear();
for (int i = 0; i < allTargetsList.Count; ++i)
{
EntityScript e = allTargetsList[i];
float newZScale = 1 - e.transform.position.z / camZ;
float newXPosition = camX + (v3.x - camX) * newZScale;
float newYPosition = camY + (v3.y - camY) * newZScale;
float xDiff = e.transform.position.x - newXPosition;
float yDiff = e.transform.position.y - newYPosition;
float sqDist = xDiff * xDiff + yDiff * yDiff;
float sizeDiff = e.hitRadius + cursorRadius * newZScale;
if (sqDist < sizeDiff * sizeDiff)
{
targets.Add(e);
}
}
sortEntityList(targets);
}
//....
public void sortEntityList(List<EntityScript> targets)
{
targets.Sort(delegate (EntityScript a, EntityScript b)
{
return a.sqDistanceFromMouse().CompareTo(b.sqDistanceFromMouse());
});
}
//....
}
I’m not sure how to implement this in ECS with Burst on, as editing of most arrays is not allowed in parallel burst jobs. How should I translate this targeting function to ECS?