- Home /
Making a perimeter for AI Agents, why are they not being detected?
I'm having difficulty in doing a particular bit of logic. I want any number of AI to make decisions based on their proximity to a particular object.
If they are within a certain limit, they will act a certain way, and if they are within an even smaller limit, they will react yet another way.
The issue is, I'm not exactly sure how to do this.
I've created a script that allows us to choose what kind of perimeter around the object we want (it varies between levels).
As a simple attempt, I've filled out script which creates a box around a central point.
To test AI interaction, I placed several object transforms in an array and tried to get the script to detect those objects.
However, the script never detects them.
I suspect there's something wrong in my logic but I could really use help as far as the organization of this code in general also. If anyone has a better solution for my problem then what I've come up with, I'm all ears.
Specifically though I want to know WHY these objects aren't being detected by my proximity script.
I've taken out the defining of the box's vectors in this code snippet.
private void RectangleLogic() { for (int i = 0; i < interactingAgent.Length; i++) { if (interactingAgent[i]) { if ( interactingAgent[i].transform.position.x > patrolBottomLeftFront.x && interactingAgent[i].transform.position.x < patrolBottomRightFront.x && interactingAgent[i].transform.position.z > patrolBottomLeftFront.z && interactingAgent[i].transform.position.z < patrolBottomLeftFront.z && interactingAgent[i].transform.position.y > perimeterCenter.transform.position.y && interactingAgent[i].transform.position.y < perimeterCenter.transform.position.y + height ) { Debug.Log("Patrol State Activated for " + interactingAgent[i].name); } else if ( interactingAgent[i].transform.position.x > chaseBottomLeftFront.x && interactingAgent[i].transform.position.x < chaseBottomRightFront.x && interactingAgent[i].transform.position.z > chaseBottomLeftFront.z && interactingAgent[i].transform.position.z < chaseBottomLeftFront.z && interactingAgent[i].transform.position.y > perimeterCenter.transform.position.y && interactingAgent[i].transform.position.y < perimeterCenter.transform.position.y + height ) { Debug.Log("Chase State Activated for " + interactingAgent[i].name); } } } }
Answer by tiredamage42 · Jul 07, 2018 at 06:42 PM
Try using unity's Bounds struct. You can set up the area using bounds.center and bounds.size. And then figure out if any position is within the bounds using bounds.contains(position)
Or better yet just change the state basedon the distance to the object. Unless you need the area around the agent to be rectangular.