79 lines
2.4 KiB
C#
79 lines
2.4 KiB
C#
using BehaviorDesigner.Runtime;
|
|
using BehaviorDesigner.Runtime.Tasks.Movement;
|
|
using UnityEngine;
|
|
#if !(UNITY_5_1 || UNITY_5_2 || UNITY_5_3 || UNITY_5_4)
|
|
using UnityEngine.AI;
|
|
#endif
|
|
|
|
namespace Valheim
|
|
{
|
|
public abstract class NavMeshGroupMovement2 : GroupMovement
|
|
{
|
|
[Tooltip("All of the agents")]
|
|
public SharedGameObjectList agents = null;
|
|
[Tooltip("The speed of the agents")]
|
|
public SharedFloat speed = 10;
|
|
[Tooltip("The angular speed of the agents")]
|
|
public SharedFloat angularSpeed = 120;
|
|
|
|
// A cache of the NavMeshAgents
|
|
protected NavMeshAgent[] navMeshAgents;
|
|
protected Transform[] transforms;
|
|
|
|
|
|
public override void OnStart()
|
|
{
|
|
navMeshAgents = new NavMeshAgent[agents.Value.Count];
|
|
transforms = new Transform[agents.Value.Count];
|
|
for (int i = 0; i < agents.Value.Count; ++i)
|
|
{
|
|
transforms[i] = agents.Value[i].transform;
|
|
navMeshAgents[i] = agents.Value[i].GetComponent<NavMeshAgent>();
|
|
navMeshAgents[i].speed = speed.Value;
|
|
navMeshAgents[i].angularSpeed = angularSpeed.Value;
|
|
#if UNITY_5_1 || UNITY_5_2 || UNITY_5_3 || UNITY_5_4 || UNITY_5_5
|
|
navMeshAgents[i].Resume();
|
|
#else
|
|
navMeshAgents[i].isStopped = false;
|
|
#endif
|
|
}
|
|
}
|
|
|
|
protected override bool SetDestination(int index, Vector3 target)
|
|
{
|
|
if (navMeshAgents[index].destination == target)
|
|
{
|
|
return true;
|
|
}
|
|
navMeshAgents[index].isStopped = false;
|
|
return navMeshAgents[index].SetDestination(target);
|
|
}
|
|
|
|
protected override Vector3 Velocity(int index)
|
|
{
|
|
return navMeshAgents[index].velocity;
|
|
}
|
|
|
|
public override void OnEnd()
|
|
{
|
|
// Disable the nav mesh
|
|
for (int i = 0; i < navMeshAgents.Length; ++i)
|
|
{
|
|
if (navMeshAgents[i] != null)
|
|
{
|
|
#if UNITY_5_1 || UNITY_5_2 || UNITY_5_3 || UNITY_5_4 || UNITY_5_5
|
|
navMeshAgents[i].Stop();
|
|
#else
|
|
navMeshAgents[i].isStopped = true;
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
|
|
// Reset the public variables
|
|
public override void OnReset()
|
|
{
|
|
agents = null;
|
|
}
|
|
}
|
|
} |