123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using BehaviorDesigner.Runtime.Tasks;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- using BehaviorDesigner.Runtime.Tasks.EnemyAI;
- namespace BehaviorDesigner.Runtime.Tasks
- {
- [TaskDescription("远程警戒,前后游走")]
- [TaskCategory("YLSJ")]
- public class ActionSurroundAlertYC : NavMeshMovementBase
- {
- [Tooltip("围绕的目标")]
- public SharedTransform target;
- [Tooltip("警戒的最小总时间")]
- public SharedFloat alertMinTime = 4.0f;
- [Tooltip("警戒的最大总时间")]
- public SharedFloat alertMaxTime = 8.0f;
- [Tooltip("在当前位置后退的最小距离(比如攻击距离是2,后退的距离是1,怪物和人物之间的实际距离是2+1=3,实际距离不要超过alertDistance距离)")]
- public SharedFloat backMinDis = 2;
- [Tooltip("在当前位置后退的最大距离(比如攻击距离是2,后退的距离是3,怪物和人物之间的实际距离是2+3=5,实际距离不要超过alertDistance距离)")]
- public SharedFloat backMaxDis = 4;
- [Tooltip("警戒的最大范围,超过这个范围则进行追逐)")]
- public SharedFloat alertDistance = 10;
- [Tooltip("停留的最小时间(后退完成/围绕完成 都会停留一小段时间)")]
- public SharedFloat stayMinTime = 1.0f;
- [Tooltip("停留的最大时间(后退完成/围绕完成 都会停留一小段时间)")]
- public SharedFloat stayMaxTime = 2.0f;
- [Tooltip("旋转速度(看向目标的旋转速度)")]
- public float rotationSpeed = 10;
- /// <summary>
- /// 出生地的警戒范围 (从出生地算,超出这个范围则不再追击目标)
- /// </summary>
- private float liveAlertDistance = 0;
- /// <summary>
- /// 攻击距离
- /// </summary>
- private float attackDistance = 0;
- /// <summary>
- /// 是否后退中
- /// </summary>
- private bool backing = true;
- private float curAroundTotalTime = 0;
- private float curStayTime = 0;
- private float curBackDis = 0;
- private BehaviorTree behaviorTree = null;
- private Role role = null;
- private bool bSeek = false;
- public override void OnAwake()
- {
- base.OnAwake();
- behaviorTree = gameObject.GetComponent<BehaviorTree>();
- role = gameObject.GetComponent<Role>();
- }
- public override void OnStart()
- {
- base.OnStart();
- liveAlertDistance = (float)behaviorTree.GetVariable("liveAlertDistance").GetValue();
- attackDistance = (float)behaviorTree.GetVariable("attackDistance").GetValue();
- curAroundTotalTime = Random.Range(alertMinTime.Value, alertMaxTime.Value);
- backing = true;
- bSeek = false;
- m_NavMeshAgent.component.updateRotation = false;//禁用旋转
- m_NavMeshAgent.component.isStopped = false;
- Vector3 dir = target.Value.position - m_NavMeshAgent.component.transform.position;
- dir.Normalize();
- curBackDis = Random.Range(backMinDis.Value, backMaxDis.Value);
- Vector3 backPos = m_NavMeshAgent.component.transform.position - dir * curBackDis;
- SetDestination(backPos);
- }
- public override TaskStatus OnUpdate()
- {
- // 目标死亡
- if (target.Value == null)
- {
- return TaskStatus.Success;
- }
- // 时间到达
- curAroundTotalTime -= time.deltaTime;
- if (curAroundTotalTime <= 0)
- {
- return TaskStatus.Success;
- }
- float liveDis = Vector3.Distance(transform.position, role.mLivePos);
- float targetDis = Vector3.Distance(transform.position, target.Value.position);
- // 超出出生地活动范围
- if (liveDis > liveAlertDistance)
- {
- return TaskStatus.Success;
- }
- Vector3 centerPos = target.Value.position;
- Vector3 dir = transform.position - centerPos;
- float dis = Vector3.Distance(transform.position, centerPos);
- dir.Normalize();
- dir.y = 0;
- // 旋转
- transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(-dir), rotationSpeed * Time.deltaTime);
- // 超出 警戒范围
- if (targetDis > alertDistance.Value)
- {
- backing = false;
- bSeek = true;
- m_NavMeshAgent.component.updateRotation = true;
- m_NavMeshAgent.component.speed = (float)behaviorTree.GetVariable("runSpeed").GetValue();
- }
- // 停留期间不做任何处理
- if (curStayTime > 0)
- {
- if(role.isDie)
- {
- return TaskStatus.Success;
- }
- if (m_NavMeshAgent.component.isOnNavMesh && m_NavMeshAgent.component.isStopped != true)
- {
- m_NavMeshAgent.component.isStopped = true;
- }
- curStayTime -= time.deltaTime;
- return TaskStatus.Running;
- }
- else
- {
- if (role.isDie)
- {
- return TaskStatus.Success;
- }
- if (m_NavMeshAgent.component.isOnNavMesh && m_NavMeshAgent.component.isStopped != false)
- {
- m_NavMeshAgent.component.isStopped = false;
- }
- }
- // 后退
- if (backing)
- {
- if (HasArrived())
- {
- ResetPostion();
- }
- }
- // 围绕目标
- else
- {
- if (bSeek == false)
- {
- if (HasArrived())
- {
- ResetPostion();
- }
- }
- else
- {
- if (targetDis > curBackDis + attackDistance)
- {
- Vector3 targetDir = target.Value.position - m_NavMeshAgent.component.transform.position;
- targetDir.Normalize();
- SetDestination(target.Value.position + targetDir * (curBackDis + attackDistance));
- }
- else
- {
- bSeek = false;
- m_NavMeshAgent.component.updateRotation = false;
- m_NavMeshAgent.component.speed = (float)behaviorTree.GetVariable("moveSpeed").GetValue();
- ResetPostion();
- }
- }
- }
- return TaskStatus.Running;
- }
- /// <summary>
- /// 重置位置
- /// </summary>
- private void ResetPostion()
- {
- Vector3 dir = target.Value.position - m_NavMeshAgent.component.transform.position;
- dir.Normalize();
- if (backing)
- {
- backing = false;
- Vector3 frontPos = m_NavMeshAgent.component.transform.position + dir * curBackDis;
- SetDestination(frontPos);
- }
- else
- {
- backing = true;
- curBackDis = Random.Range(backMinDis.Value, backMaxDis.Value);
- Vector3 backPos = m_NavMeshAgent.component.transform.position - dir * curBackDis;
- SetDestination(backPos);
- }
- curStayTime = Random.Range(stayMinTime.Value, stayMaxTime.Value);
- }
- public override void OnEnd()
- {
- base.OnEnd();
- if (m_NavMeshAgent.component.isOnNavMesh)
- {
- m_NavMeshAgent.component.isStopped = true;
- m_NavMeshAgent.component.updateRotation = true;
- }
- }
- }
- }
|