利用NavMeshAgent控制敌人巡逻,即敌人在一组位置间循环巡逻。

首先我们要知道NavMeshAgent中有两个方法:1.锁定当前巡逻的某一目标位置,即navMeshAgent.destination

2.到达目标位置停止巡逻(休息一定时间继续巡逻),即navMeshAgent.Stop();

代码实现如下:

usingUnityEngine;

usingSystem.Collections;

using UnityEngine.AI;

public class EnemyMoveAI : MonoBehaviour {

public Transform[] directPoints; //0-3

privateint index = 0;

public float patroTime = 3f;//到达某一点停止等待时间

private float timer = 0;//计时器

privateNavMeshAgentnavMeshAgent;

void Awake()

{

navMeshAgent = GetComponent<NavMeshAgent>();

navMeshAgent.destination = directPoints[index].position;

}

void Update()

{

if (navMeshAgent.remainingDistance< 0.5f)

{

timer += Time.deltaTime;

if (timer==patroTime)

{

index++;

index %= 4;//在4个点之间循环巡逻

timer = 0;

navMeshAgent.destination = directPoints[index].position;

}

}

}

}

注意:

1.为了保证敌人能在该组点内循环巡逻(而不是只巡逻一圈),采用取余的方式获得点数组的下标。

2.没有使用navMeshAgent.Stop();原因是,如果使用了则到达一个巡逻点后敌人将不会再向新的目标点移动。

最新文章

  1. 【模式匹配】更快的Boyer-Moore算法
  2. 深入理解 C# 协变和逆变
  3. dedecms后台登录如何去除验证码设置
  4. js和jQuery 获取屏幕高度、宽度
  5. Freezable 对象(WPF)
  6. TeamViewer
  7. 图的建立——邻接矩阵表示(C语言+VC6.0平台)
  8. codecomb 2085【肥得更高】
  9. LeetCode 100. Same Tree (相同的树)
  10. Laravel添加代码自动提示功能
  11. ROS_Kinetic_05 ROS基础内容(二)
  12. [再寄小读者之数学篇](2014-05-28 Ladyzhenskaya 不等式)
  13. layer —— 一个简单的jQuery弹出层插件
  14. centos 7 安装mqtt 修改用户名和密码
  15. 【转】 UI自动化测试的关注点
  16. react优化--pureComponent
  17. 认识TWICImage类
  18. Eclipse折叠代码 coffee bytes code folding
  19. Spark_安装配置_运行模式
  20. 【数据结构】 List 简单实现

热门文章

  1. Windows系统配置java环境
  2. mysql查看视图用户
  3. Spring MVC中的 权限拦截定义 以及 权限拦截的研发步骤
  4. web设计_3_可伸缩的导航栏
  5. Codeforces Round #192 (Div. 2) (330B) B.Road Construction
  6. netty使用EmbeddedChannel对channel的出入站进行单元测试
  7. 【有容云】PPT | 容器落地之二三事儿
  8. Linux基础文件打包
  9. Netty服务端启动过程相关源码分析
  10. 高性能MySQL之基础架构