- Home /
Question about AI in MMORPG
Hi guys! I'm currently making a Co-op Zombie Apocalypse Survival Game.
But there's on problem. The Zombie follow just the newest GameObject with "Player" Tag. not the old one :/ Is there any way to fix it?
Btw Here's the Script i tried:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
public int minRange;
public bool follow;
public float moveSpeed;
public float rotationSpeed;
private Transform myTransform;
public Transform player;
void Start()
{
myTransform = transform;
player = null;
}
void Update ()
{
if(Vector3.Distance(GameObject.FindWithTag("Player").transform.position, myTransform.position) < minRange)
{
player = GameObject.FindWithTag("Player").transform;
follow=true;
}else{
follow=false;
}
if(follow && player != null)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(player.position - myTransform.position), rotationSpeed * Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}
Any Ideas :)?
Stop finding the player tag every update(if within distance). Create (enum) a state of the zombie, if it has a target, let it follow, if not, search for one within distance, otherwise go to idle or randomly walk around. FindWithTag is going to return the first instance it finds.
Answer by hvilela · Oct 19, 2012 at 10:57 PM
You can move your FindWithTag command to the Start method and save it in a variable. This way you're zombie will aways follow the same player. But the really cool thing to do here is to use GameObject.FindGameObjectsWithTag to get all players and selected the nearest or the weakest player, so you can start to develop an AI.
Your answer
Follow this Question
Related Questions
Disable a target after trigger exit? 1 Answer
How do I make enemies and health? 2 Answers
Stay Back! 2 Answers
Player pushback when collide with enemy 2 Answers
Shooting Damage Help 1 Answer