Hello,
I am trying to use the project to make objects randomly move around the background of a scene.
I have multiple target locations (gameobjects with a circle collider 2D and isTrigger = true). I have created a list of these target locations and when my object is spawned, it selects a random target from the list and makes that the destination. When the object triggers the collider on the target, I am executing some code to select a new target from the list and making that the destination. This works fine for a variable number of times, but at some point the objects get āstuckā. This could be after 2 paths or 20, it varies. The targets are not in zones where the object cannot reach (I have checked the graphs) and the circle collider is triggered to run the code (checked with debug).
I am not sure why it would not keep selecting new paths? My code that is executed when the object hits the circle collider:
private void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Target" && other.GetComponent<UI_Target>().targetID == targetID) {
target = null;
aiPath.SetPath(null);
int target = Random.Range(0, _levelSelect.possibleTargets.Count);
target = _levelSelect.possibleTargets[target];
TargetID = target.GetComponent<UI_Target>().targetID;
}
}
I have the Seeker and AIPath components added to my object and am using the following code to update my path:
private void UpdatePath() {
aiPath.destination = target.transform.position;
}
This code is executed using InvokeRepeating in Start function: InvokeRepeating(āUpdatePathā, 0f, 0.5f);
Is there something wrong with my code? It executes a few times before it breaks, so I am not sure what the issue could be? Any advice would be greatly appreciated.
Many Thanks,
John