unityscript - Unity3D - Multiple enemies patrolling multiple paths in the same script -
i'm new writing code. enemy patrol between 2 paths. when player enters detection area around enemy follow player.
if wanted more 1 enemy , multiple paths, how add code able that. creating script each enemy waste, correct?
public var enemypath1 : transform; public var enemypath2 : transform; private var target : transform = null; private var charactercontrols: charactercontroller; function start () { settarget(enemypath1); charactercontrols = getcomponent(charactercontroller); } function settarget(newtarget : transform) : void { target = newtarget; } function update() : void { var lookatposition : vector3 = vector3(target.position.x, this.transform.position.y, target.position.z); transform.lookat(lookatposition); charactercontrols.simplemove(transform.forward); } function ontriggerenter(node : collider) : void { if(node.transform == target) { if(target == enemypath1) { settarget(enemypath2); } else if(target == enemypath2) { settarget(enemypath1); } } }
this code supposed go inside enemy gameobject, right? 1 way of doing put desired paths inside array, in order supposed followed enemy.
var paths : transform[]; var pathindex : int = 0; // in start function: function start () { settarget(paths[pathindex]); charactercontrols = getcomponent(charactercontroller); } // in function doing checking , looking next path function ontriggerenter(node : collider) : void { if(node.transform == target) { // increment index looks next path object pathindex += 1; if(pathindex == paths.length) { // resets first path if last 1 pathindex = 0; } settarget(paths[pathindex]); } }
so can add script enemy gameobjects in scene, in editor can manually set paths variable whatever paths wish in desired order, , should follow set routine.
Comments
Post a Comment