- Home /
Enemy follow tag
I am trying to make a multiplayer game, and the enemy is supposed to follow the player. The player is spawned, so Transform wont work. I am trying to change this script to do that, but I cant seem to get it to work:
var speed : int = 0; var followrange : int; var stoprange : int; static var canmove : boolean = true; var target : Transform; private var stopped : boolean = false;
function Update(){
if(Vector3.Distance(target.position, transform.position) <= followrange && stopped == false) { canmove = false; transform.LookAt(target); transform.Translate(Vector3(0,0,1) speed Time.deltaTime); }
if(Vector3.Distance(target.position, transform.position) <= stoprange) { canmove = false; transform.LookAt(target); stopped = true; }
if(Vector3.Distance(target.position, transform.position) > stoprange) { stopped = false; }
if(Vector3.Distance(target.position, transform.position) > followrange) { canmove = true; } }
Answer by Zarenityx · Jan 01, 2013 at 05:57 PM
Actually, transform will work. When you call the instantiate function, it will create an instance of the object specified and will return the GameObject of that specific object. I would recommend creating a master script that both instantiates the players and catalogs their transforms in an array. Then have the enemies reference the master script(static variables are a bad practice in multiplayer games; they cause a ton of problems) and performs calculations to find which is the best to attack, or possibly just set as your 'target' variable. Another option is to use GameObject.Find(), but it is a slow process with a high level of failure, and generally caching the players in an array when they are instantiated is a much faster and more accurate process. Both of these methods work for spawned enemies as well as non-spawned (enabled/disabled) enemies.
Your answer
Follow this Question
Related Questions
How To Make Enemy Move Around 0 Answers
Navmesh follow and stop at a distance 2 Answers
Destroy and Spawn an Enemy 1 Answer
Spawning Objects at location 3 Answers
Unet spawn a camera? 0 Answers