RPG_类暗黑破坏神 99 人阅读 | 0 人回复
70
0
308
管理员
using RPG.Combat; using System.Collections; using System.Collections.Generic; using UnityEngine; using RPG.Movement; using RPG.Core; using System; namespace RPG.Control { //AI的控制 public class AIController : MonoBehaviour { [SerializeField] float chaseDistance = 5f;// 设定追逐范围的距离 [SerializeField] float suspicionTime = 4f;//丢失目标后原地等待时间 [SerializeField] float waypointDwellTime = 3f;//倒地巡逻点之后等待时间 [SerializeField] PatrolPath patrolPath;//巡逻点 [SerializeField] float waypointTolerance = 1f;//到达巡逻点允许的误差范围 float timeSinceLastSawPlayer = Mathf.Infinity;//最后一次看见玩家的时间 时间计时器 设置无限大 float timeSinceArrivedAtWaypoint = Mathf.Infinity;//到达巡逻点后 时间计时器 设置无限大 int currentWaypointIndex = 0;//当前巡逻点数量 Vector3 guardPosition;//默认位置 //组件 Fighter fighter; Health health; Mover mover; GameObject player; private void Start() { fighter = GetComponent<Fighter>(); health = GetComponent<Health>(); player = GameObject.FindWithTag("Player");// 找到标签为“Player”的游戏对象 mover = GetComponent<Mover>(); guardPosition = transform.position; } private void Update() { if (health.IsDead()) return;//如果角色已经死亡,则跳出 // 如果和玩家的距离小于追逐范围 并且可以攻击玩家 if (InAttackRangeOfPlayer() && fighter.CanAttack(player)) { AttackBehaviour();//开始攻击 } else if (timeSinceLastSawPlayer < suspicionTime || timeSinceArrivedAtWaypoint < waypointDwellTime)//失去对玩家的视线后,怀疑时间 { SuspicionBehaviour();//原地待机 } else { PatrolBehaviour();//进入巡逻 } UpdateTimes(); } private void UpdateTimes()//计时器更新 { timeSinceLastSawPlayer += Time.deltaTime; timeSinceArrivedAtWaypoint += Time.deltaTime; } private void PatrolBehaviour()//巡逻行为 { Vector3 nectPoition = guardPosition; // 默认巡逻点设为守卫的起始位置 if (patrolPath != null)// 如果存在巡逻路径 { if(AtWaypoint())// 如果到达当前巡逻点 { timeSinceArrivedAtWaypoint = 0f;//重置巡逻计时器为0 CycleWaypoint();// 切换到下一个巡逻点 } nectPoition = GetCurrentWaypoint();// 获取当前巡逻点的位置 } if(timeSinceArrivedAtWaypoint >waypointDwellTime) { mover.StartMoveAction(nectPoition);// 开始移动到当前巡逻点 } } private bool AtWaypoint()// 检查是否到达巡逻点 { // 计算当前位置与当前巡逻点之间的距离 float distanceToWaypoint = Vector3.Distance(transform.position,GetCurrentWaypoint()); return distanceToWaypoint <waypointTolerance; // 判断是否小于允许的误差范围 } private void CycleWaypoint()// 切换到下一个巡逻点 { // 更新当前巡逻点的索引 currentWaypointIndex = patrolPath.GetNextIndex(currentWaypointIndex); } private Vector3 GetCurrentWaypoint()// 获取当前巡逻点的位置 { // 从巡逻路径中获取当前巡逻点的位置 return patrolPath.GetWaypoint(currentWaypointIndex); } private void SuspicionBehaviour()//丢失目标后,取消当前任何行为 { GetComponent<ActionScheduler>().CancelCurrentAction(); } private void AttackBehaviour()//攻击行为 { timeSinceLastSawPlayer = 0;//重置为0 fighter.Attack(player);//对玩家进行攻击 } private bool InAttackRangeOfPlayer() // 检测是否在攻击范围内 { float distanceToPlayer = Vector3.Distance(player.transform.position,transform.position); // 计算并返回玩家和当前对象之间的距离 return distanceToPlayer < chaseDistance;//返回两者之间距离是否小于追逐距离 } private void OnDrawGizmosSelected()//绘制线框球体 { Gizmos.color = Color.yellow; Gizmos.DrawWireSphere(transform.position, chaseDistance); } } } 复制代码
举报
本版积分规则 发表回复 回帖后跳转到最后一页
主题
帖子
积分