- Home /
Using box collider to determine if AI should stop help
Hey guys. Situation - I have civilian AI roam around randomly from waypoint to waypoint - they use raycast to avoid one another - which works pretty decent, but sometimes 2 AI reach the node around the same time, and also attempt to head to the next waypoint at the same time, they stack into each other... I attempted to put a box collider on them as a child so they would stop if following to close until the exit happens from the other one they are following, and to prevent them from stacking into one another, but this just generated a few more problems.
AI will now run into each other, the colliders will both register and the never-ending face off happens. I lack the logic on my part to figure out a system where the AI will stop and allow another to move away, and then keep on his path... because they both are using the same collider on the same layer.
Here is a simple snip of the script I'm using to talk to my AI script (just using the speed var in there to force the stop)
using UnityEngine;
using System.Collections;
public class AIStop : MonoBehaviour {
private bool wait;
public AI ai;
void Start () {
wait = false;
}
void Update () {
if (wait)
{
ai.speed = 0f;
}
else{
ai.speed = 5f;
}
}
void OnTriggerEnter(Collider AIPED)
{
wait = true;
}
void OnTriggerExit(Collider AIPED)
{
wait = false;
}
}
Without this weak attempt a collider if I keep the population low they appear to work fine because the chance of them getting to the same waypoint at the same time is low - but thats a workaround, not a solution... just interested to hear other peoples approaches and ideas.
Answer by markedagain · Jul 11, 2013 at 04:26 PM
Hey ,
Since you know who you are colliding with for example AIPED then you can do something of the sort
if (AIPED.getComponent<AIStop>().wait == true){
wait =false;
}else{
wait = true;
}
You are about to hit a getComponent is not a member of Collider and then a NullReferenceException on the second and fourth line. But more than that even if you store the component you are still about to get a "wait is not accessible due to its protection level" (or something like this) You need to store the component reference and simply:
AIStop script = AIPED.transform.GetComponent<AIStop>();
script.wait = !script.wait;
and make sure wait is public to be seen outside the class or use a getter method.
ya its sad how much i rely on auto complete, but i was just showing the logic behind it. i dont like this method anyway as u cant control who does the actual waiting, but u can simply put some distance check to see who is closer and have the other person wait
Your answer
Follow this Question
Related Questions
WayPoints mixed with Raycast 1 Answer
Not sure what this error means? 1 Answer
How to create waypoint between two gameobjects according to variable value? 0 Answers
AI Evasion help 1 Answer
Way Point Script Change Group 0 Answers