unity_小功能实现(敌人巡逻功能)
利用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();原因是,如果使用了则到达一个巡逻点后敌人将不会再向新的目标点移动。
最新文章
- 【模式匹配】更快的Boyer-Moore算法
- 深入理解 C# 协变和逆变
- dedecms后台登录如何去除验证码设置
- js和jQuery 获取屏幕高度、宽度
- Freezable 对象(WPF)
- TeamViewer
- 图的建立——邻接矩阵表示(C语言+VC6.0平台)
- codecomb 2085【肥得更高】
- LeetCode 100. Same Tree (相同的树)
- Laravel添加代码自动提示功能
- ROS_Kinetic_05 ROS基础内容(二)
- [再寄小读者之数学篇](2014-05-28 Ladyzhenskaya 不等式)
- layer —— 一个简单的jQuery弹出层插件
- centos 7 安装mqtt 修改用户名和密码
- 【转】 UI自动化测试的关注点
- react优化--pureComponent
- 认识TWICImage类
- Eclipse折叠代码 coffee bytes code folding
- Spark_安装配置_运行模式
- 【数据结构】 List 简单实现
热门文章
- Windows系统配置java环境
- mysql查看视图用户
- Spring MVC中的 权限拦截定义 以及 权限拦截的研发步骤
- web设计_3_可伸缩的导航栏
- Codeforces Round #192 (Div. 2) (330B) B.Road Construction
- netty使用EmbeddedChannel对channel的出入站进行单元测试
- 【有容云】PPT | 容器落地之二三事儿
- Linux基础文件打包
- Netty服务端启动过程相关源码分析
- 高性能MySQL之基础架构