using UnityEngine;
using System.Collections;
public class enemyController : MonoBehaviour {
public float distanceToMe; //智能体到目标的距离
public GameObject me; //目标角色
public float isSeekDistance = 10.0f; //可靠近范围
public int state; //智能体状态
void Start()
{
me = GameObject.FindWithTag ("Player");
}
利用Switch()可以添加其他功能,如攻击,逃跑等;
void Update()
{
switch (state)
{
case 0:
Idle(); //空闲,四处游荡
break;
case 1:
Seek(); //向目标靠近
break;
}
}
//智能体空闲函数
void Idle()
{
//获取两者间的距离
distanceToMe = Vector3.Distance( me.transform.position ,this.transform .position);
if (distanceToMe > isSeekDistance) //大于可靠近范围,进入空闲状态
{
state = 0;
if(Random.value >0.5) //通过随机值,使其随机左右移动
{
this.transform.Rotate(Vector3.up*5);
}
else
{
trans