loops - How how can I make AI only move towards objects that are active in the scene(Unity Engine) -


hi i'm working on restaurant strategy game customers come in , buy things. have created array of transform type objects act way points , customer move towards way points active in scene. i'm thinking of doing using loop or while loop every way point not active, move next way point , check see if active. i'm not sure if correct way because can't seem work. anyways use 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; } } 

it's bad idea™ in game programming when find taking solution of "every time this, i'll iterate on objects , check if they're active..." once upon time made mistake on 100000x100000 grid. best solution problems of type inverting question. instead of asking each waypoint whether it's active, each waypoint should ask whether belongs active or inactive set. i'd recommend having @ least 2 lists: activewaypoints , inactivewaypoints. if need (and do), masterwaypoints can used universal set.

now it's simple: each waypoint knows whether it's active or inactive, when activates removes inactive list , adds active list , vice versa. when it's created, have choose whether it's active or inactive default (or provide option create either default), that's minor design issue @ best. when it's destroyed, remove whichever , masterwaypoints if exists.

the benefit have update things when change state, rather check them every frame (or worse: every npc agent every frame).

from there, well, have each customer check activewaypoints. if need further filtering each customer, gets little more difficult. in case may acceptable iterate on activewaypoints , ignore waypoints don't meet criteria. though depending on how filtering needs done, i'd consider writing sort of waypointmanager own logic filtering based on active/inactive/red/blue/made-of-marmalade/insanity-inducing/etc that's accessed via getwaypointsmeetingcriteria(list of filters). can use hybrid of maps, lists, , filtering via iteration depending on decide each tag.

there snag here in waypoints are, presumably, ordered, means when waypoints move inactive or active need have order maintained, there plenty of methods inserting and/or constructing sorted list.


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -