unity官方demo学习之Stealth(二十四)敌人AI
2015-10-05 20:08
507 查看
1,添加脚本文件DoneEnemyAI
参数:(公有)
敌人巡逻速度
敌人追踪速度
到达追踪地点后的等待时间
到达每个巡逻路径点后的等待时间
储存路径点坐标的数组
(私有)
EnemySight脚本引用
nav
mesh agent组件引用
2,将敌人拖入prefab,复制两个,位置分别是-19,0,37;-29,0,43.5.为这三个敌人排序_001,_002,_003
3,给敌人设置路径巡逻点。创建一个空对象wayPoints(reset position),复制1个命名为wayPoint_001,使用2D Gizmos来标记空对象
选中wayPoint_001,点击inspector视图左上角的立方体,选择other,选择wayPoint_001,然后会发现wayPoint_001,
被标记为蓝色小旗,接下来复制10个wayPoint_001,分别命名为_002到_011,如图023,
接下来分别给这11ge设置position
首先是第一个敌人巡逻点,图024,
为第一个敌人char_robotGuard_002添加1到4的点,如图027
第二个敌人,图025,添加5到8的点
第三个,图026,
添加剩余的点,不过顺序为9,8,10,11,因为2,3敌人共用8这个点,如图028
using UnityEngine; using System.Collections; public class DoneEnemyAI : MonoBehaviour { public float patrolSpeed = 2f; // The nav mesh agent's speed when patrolling. public float chaseSpeed = 5f; // The nav mesh agent's speed when chasing. public float chaseWaitTime = 5f; // The amount of time to wait when the last sighting is reached. public float patrolWaitTime = 1f; // The amount of time to wait when the patrol way point is reached. public Transform[] patrolWayPoints; // An array of transforms for the patrol route. private DoneEnemySight enemySight; // Reference to the EnemySight script. private NavMeshAgent nav; // Reference to the nav mesh agent. private Transform player; // Reference to the player's transform. private DonePlayerHealth playerHealth; // Reference to the PlayerHealth script. private DoneLastPlayerSighting lastPlayerSighting; // Reference to the last global sighting of the player. private float chaseTimer; // A timer for the chaseWaitTime. private float patrolTimer; // A timer for the patrolWaitTime. private int wayPointIndex; // A counter for the way point array. void Awake () { // Setting up the references. enemySight = GetComponent<DoneEnemySight>(); nav = GetComponent<NavMeshAgent>(); player = GameObject.FindGameObjectWithTag(DoneTags.player).transform; playerHealth = player.GetComponent<DonePlayerHealth>(); lastPlayerSighting = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent<DoneLastPlayerSighting>(); } void Update () { // If the player is in sight and is alive... if(enemySight.playerInSight && playerHealth.health > 0f) // ... shoot. Shooting(); // If the player has been sighted and isn't dead... else if(enemySight.personalLastSighting != lastPlayerSighting.resetPosition && playerHealth.health > 0f) // ... chase. Chasing(); // Otherwise... else // ... patrol. Patrolling(); } void Shooting () { // Stop the enemy where it is. nav.Stop(); } void Chasing () { // Create a vector from the enemy to the last sighting of the player. Vector3 sightingDeltaPos = enemySight.personalLastSighting - transform.position; // If the the last personal sighting of the player is not close... if(sightingDeltaPos.sqrMagnitude > 4f) // ... set the destination for the NavMeshAgent to the last personal sighting of the player. nav.destination = enemySight.personalLastSighting; // Set the appropriate speed for the NavMeshAgent. nav.speed = chaseSpeed; // If near the last personal sighting... if(nav.remainingDistance < nav.stoppingDistance) { // ... increment the timer. chaseTimer += Time.deltaTime; // If the timer exceeds the wait time... if(chaseTimer >= chaseWaitTime) { // ... reset last global sighting, the last personal sighting and the timer. lastPlayerSighting.position = lastPlayerSighting.resetPosition; enemySight.personalLastSighting = lastPlayerSighting.resetPosition; chaseTimer = 0f; } } else // If not near the last sighting personal sighting of the player, reset the timer. chaseTimer = 0f; } void Patrolling () { // Set an appropriate speed for the NavMeshAgent. nav.speed = patrolSpeed; // If near the next waypoint or there is no destination... if(nav.destination == lastPlayerSighting.resetPosition || nav.remainingDistance < nav.stoppingDistance) { // ... increment the timer. patrolTimer += Time.deltaTime; // If the timer exceeds the wait time... if(patrolTimer >= patrolWaitTime) { // ... increment the wayPointIndex. if(wayPointIndex == patrolWayPoints.Length - 1) wayPointIndex = 0; else wayPointIndex++; // Reset the timer. patrolTimer = 0; } } else // If not near a destination, reset the timer. patrolTimer = 0; // Set the destination to the patrolWayPoint. nav.destination = patrolWayPoints[wayPointIndex].position; } }
参数:(公有)
敌人巡逻速度
敌人追踪速度
到达追踪地点后的等待时间
到达每个巡逻路径点后的等待时间
储存路径点坐标的数组
(私有)
EnemySight脚本引用
nav
mesh agent组件引用
2,将敌人拖入prefab,复制两个,位置分别是-19,0,37;-29,0,43.5.为这三个敌人排序_001,_002,_003
3,给敌人设置路径巡逻点。创建一个空对象wayPoints(reset position),复制1个命名为wayPoint_001,使用2D Gizmos来标记空对象
选中wayPoint_001,点击inspector视图左上角的立方体,选择other,选择wayPoint_001,然后会发现wayPoint_001,
被标记为蓝色小旗,接下来复制10个wayPoint_001,分别命名为_002到_011,如图023,
接下来分别给这11ge设置position
首先是第一个敌人巡逻点,图024,
为第一个敌人char_robotGuard_002添加1到4的点,如图027
第二个敌人,图025,添加5到8的点
第三个,图026,
添加剩余的点,不过顺序为9,8,10,11,因为2,3敌人共用8这个点,如图028
相关文章推荐
- unity官方demo学习之Stealth(二十三)敌人射击功能
- unity3D 4.6与上述号码. UI穿透问题,而且不穿透的真机模拟器渗透问题解决
- unity官方demo学习之Stealth(二十二)敌人动画控制
- Unity5中lightmap的坑
- unity coc 战斗回放总结
- 全面理解Unity加载和内存管理
- (七)unity4.6Ugui中国教程文档-------摘要-UGUI Auto Layout
- unity官方demo学习之Stealth(二十一)敌人动画设置类
- unity官方demo学习之Stealth(二十)敌人视听范围
- unity官方demo学习之Stealth(十九)敌人动画控制
- unity官方demo学习之Stealth(十八)敌人初步设定
- unity官方demo学习之Stealth(十七)电梯移动设定
- unity官方demo学习之Stealth(十六)双层门
- unity官方demo学习之Stealth(十五)单开门动画
- unity官方demo学习之Stealth(十四)钥匙卡设定
- unity官方demo学习之Stealth(十三)摄像机跟踪
- unity官方demo学习之Stealth(十二)角色生命值
- unity官方demo学习之Stealth(十一)角色移动
- unity官方demo学习之Stealth(十)散列ID
- unity官方demo学习之Stealth(九)角色动画控制器