- Home /
How how can I make AI only move towards objects that are active in the scene
Hi I'm working on a restaurant strategy game where customers come in and buy things. I have created an array of Transform type objects that act as way points and I would like the customer to only move towards the way points that are active in the scene. What I'm thinking of doing is using a for loop or a while loop where for every way point that is not active, move to the next way point and check to see if its active. I'm not sure if this is the correct way to do it because I can't seem to get it to work. Anyways I could really use some help. Thanks!
var waypoints : Transform[];
var waypoint : Transform;
var currentWaypoint : int;
var agent : NavMeshAgent;
var script1 : SlideOutMenu;
function Awake(){
agent = gameObject.GetComponent.<NavMeshAgent>();
agent.speed = Random.Range(2.5, 5);
script1 = gameObject.Find("GUIElements").GetComponent(SlideOutMenu);
}
function Update () {
waypoint = waypoints[currentWaypoint];
agent.SetDestination(waypoint.position);
}
function OnTriggerExit(other : Collider){
if (other.name == "0 Start"){
currentWaypoint = Random.Range(1, 2);
}
}
function OnTriggerEnter (other : Collider) {
EatMeal();
if (other.name == "0 Start"){
currentWaypoint++;
}
if (other.name == "1"){
script1.globalMoney += 50;
WaitForSoda();
}
if (other.name == "2"){
script1.globalMoney += 100;
WaitForFood();
}
if (other.name == "4 End"){
Destroy(gameObject);
}
}
function WaitForSoda(){
yield WaitForSeconds(2);
currentWaypoint++;
}
function WaitForFood(){
yield WaitForSeconds(5);
currentWaypoint = Random.Range(5, 12);
}
function EatMeal(){
if (currentWaypoint >= 4 && currentWaypoint <= 12){
yield WaitForSeconds(Random.Range(5, 12));
currentWaypoint = 4;
}
}
Comment