using UnityEngine; using UnityEngine.AI; namespace BehaviorDesigner.Runtime.Formations.Tasks { public class NavMeshFormationGroup : FormationGroup { /// /// The NavMeshFormationAgent class contains component references and variables for each NavMeshAgent. /// public class NavMeshFormationAgent : FormationAgent { private NavMeshAgent navMeshAgent; public override float Speed { set { navMeshAgent.speed = value; } } public override float Radius { get { return navMeshAgent.radius; } } public override float RemainingDistance { get { return navMeshAgent.remainingDistance; } } public override float StoppingDistance { get { return navMeshAgent.stoppingDistance; } } public override bool HasPath { get { return navMeshAgent.hasPath; } } public override bool PathPending { get { return navMeshAgent.pathPending; } } public override bool AutoBreaking { set { navMeshAgent.autoBraking = value; } } /// /// Caches the component references and initialize default values. /// public NavMeshFormationAgent(Transform agent) : base(agent) { navMeshAgent = agent.GetComponent(); if (navMeshAgent.hasPath) { navMeshAgent.ResetPath(); navMeshAgent.isStopped = true; } } /// /// Resumes pathfinding. /// public override void Resume() { navMeshAgent.isStopped = false; } /// /// Sets the destination. /// public override void SetDestination(Vector3 destination) { destination.y = navMeshAgent.destination.y; if (navMeshAgent.destination != destination) { navMeshAgent.SetDestination(destination); navMeshAgent.isStopped = false; } } /// /// Rotates towards the target rotation. /// public override bool RotateTowards(Quaternion targetRotation) { if (Quaternion.Angle(transform.rotation, targetRotation) < 0.5f) { return true; } transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, navMeshAgent.angularSpeed * Time.deltaTime); return false; } /// /// Stops the agent from moving. /// public override void Stop() { if (navMeshAgent.hasPath) { navMeshAgent.isStopped = true; navMeshAgent.ResetPath(); } } /// /// The task has ended. Perform any cleanup. /// public override void End() { Stop(); navMeshAgent.updateRotation = true; navMeshAgent.velocity = Vector3.zero; } } public override void OnAwake() { base.OnAwake(); formationAgent = new NavMeshFormationAgent(transform); } public override void OnStart() { base.OnStart(); if (leader.Value != null && leaderTree != null) { leaderAgent = new NavMeshFormationAgent(leaderTree.transform); } } protected override void AddAgentToGroup(Behavior agent, int index) { base.AddAgentToGroup(agent, index); if (leader.Value == null) { formationAgents.Insert(index, new NavMeshFormationAgent(agent.transform)); } } } }