一、追击玩家
当玩家进入到了敌人的触发器范围便追击玩家
准备:四个巡逻点 一个敌人 一个玩家角色
实现步骤:(我喜欢把所有的GameObject、Transform都设为public 因为发生bug是可以直到是哪个组件或者是物体设置错了)
1、用一个Transform 类型的变量来保存巡逻点的Transform信息
public Transform infoPoint;
2、创建一个列表来保存巡逻点
public List<Transform> list;
3、 将所有的巡逻点添加到列表中
void AddToList()
{
foreach(Trasnform t in infoPoint)
{
list,Add(t);
}
}
// 这三步可以省略因为可以在游戏里通过拖拽设置巡逻点
4、设置巡逻点下标
public int location;
5、设置导航
public public NavMeshAgent agent;
agent = GetComponent<NavMeshAgent>();
6、移动至下一个巡逻点,并且在Start中调用
void Move()
{
agent.destination = list[location].position
}
7、设置多点巡逻
void Update()
{
if (agent.remainingDistance < 0.2f && !agent.pathPending) // 是敌人只有一种移动方式
{
Move();
}
}
void Move()
{
if(location,Count==0){return;}
agent.destination = list[location].position
location = (loaction + 1) % list.Count;
}
8、追击玩家
public Transform player // 追击玩家 实际上是让敌人的坐标位置变成玩家的位置
player = GameObject.FindGameObjectWithTag("Player");
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag== "Player")
{
Debug.Log("hello");
agent.destination = player.position; // 可以在此处添加销毁玩家的逻辑
}
}