-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAvoidance.cs
167 lines (133 loc) · 5.9 KB
/
Avoidance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
namespace NavMeshAvoidance
{
[DisallowMultipleComponent]
public sealed class Avoidance : MonoBehaviour
{
readonly List<NavMeshAgent> agents = new ();
readonly List<Transform> agentsTransforms = new ();
readonly List<Vector3> agentsPositions = new ();
[Tooltip("Speed of agents \"pushing\" from each other in m/s. Increase to make avoidance more noticeable. Default is 1.")]
[SerializeField, Range(0f, 300f)] float strength = 1;
[Tooltip("Agents will try to keep this distance between them. Something like NavMeshAgent.Radius, but same for all agents. Do not make this value bigger than Max Avoidance Distance.")]
[SerializeField, Range(0.1f, 15f)] float distance = 1;
[Header("Optimization")]
[Tooltip("Agents will ignore others with distance greater than this value. Bigger value can decrease performance.")]
[SerializeField, Range(0.1f, 15f)] float maxAvoidanceDistance = 3f;
[Tooltip("If enabled, will calculate avoidance only between nearest agents. Significantly improves performance for big agents crowds, but can reduce avoidance accuracy.")]
[SerializeField] bool useSpatialHash = true;
[Tooltip("World will be separated to cells, agent will know only about agents in the nearest cells to him")]
[SerializeField, Range(1, 20)] int spatialHashCellSize = 2;
[Tooltip("Smaller values increasing accuracy, but can affect performance.")]
[SerializeField, Range(0.05f, 5f)] float spatialHashUpdatePeriod = 0.5f;
[Tooltip("Estimate average agent size for your game. Usually it is in range of 1-3f. Correct value prevents spatial hash calculation errors.")]
[SerializeField, Range(0.1f, 5f)] float averageAgentSize = 1f;
[Header("Debug")]
[SerializeField] bool showDebugGizmos = true;
public float Distance => distance;
float sqrMaxAvoidanceDistance;
float sqrDistance;
SpatialHashProcessor spatialHash;
readonly Vector3 vec3Zero = Vector3.zero;
void OnValidate()
{
if (maxAvoidanceDistance < distance)
{
Debug.LogWarning("Max Avoidance Distance value should be bigger than Distance!");
maxAvoidanceDistance = distance;
}
if (spatialHashCellSize <= maxAvoidanceDistance)
{
Debug.LogWarning("Spatial Hash cell size should be bigger than Max Avoidance Distance!");
spatialHashCellSize = (int)(maxAvoidanceDistance + 1);
}
}
void Awake()
{
sqrMaxAvoidanceDistance = Mathf.Pow(maxAvoidanceDistance, 2);
sqrDistance = Mathf.Pow(distance, 2);
if (useSpatialHash)
{
spatialHash = new SpatialHashProcessor(spatialHashCellSize, averageAgentSize);
UpdatePositions();
StartCoroutine(UpdateSpatialHash());
}
}
IEnumerator UpdateSpatialHash()
{
while (true)
{
spatialHash.Rebuild(agentsPositions);
yield return new WaitForSeconds(spatialHashUpdatePeriod);
}
}
void Update()
{
UpdatePositions();
CalculateAvoidance();
}
void UpdatePositions()
{
agentsPositions.Clear();
foreach (var tr in agentsTransforms)
agentsPositions.Add(tr.position);
}
void CalculateAvoidance()
{
var agentsTotal = agents.Count;
var deltaTime = Time.deltaTime;
for (var q = 0; q < agentsTotal; q++)
{
var spatialIds = spatialHash?.NearestTargets?[q];
var otherTotal = useSpatialHash ? spatialIds!.Count : agentsTotal;
var avoidanceVector = vec3Zero;
var agentAPos = otherTotal > 0 ? agentsPositions[q] : vec3Zero;
for (var w = 0; w < otherTotal; w++)
{
var agentBIndex = useSpatialHash ? spatialIds![w] : w;
var direction = agentAPos - agentsPositions[agentBIndex];
var curSqrDistance = direction.sqrMagnitude;
if (curSqrDistance > sqrMaxAvoidanceDistance)
continue;
var weakness = curSqrDistance / sqrDistance;
if (weakness > 1f)
continue;
direction.y = 0;
avoidanceVector += Vector3.Lerp(direction * strength, vec3Zero, weakness);
}
if (showDebugGizmos)
Debug.DrawRay(agentsPositions[q], avoidanceVector, Color.green);
agents[q].Move(avoidanceVector * (strength * deltaTime));
}
}
public void AddAgent(NavMeshAgent agent)
{
var tr = agent.transform;
if (useSpatialHash)
spatialHash.Add(agents.Count); // there can be maximum value of agents.Count, so adding it as current maximum id.
agents.Add(agent);
agentsTransforms.Add(tr);
agentsPositions.Add(tr.position);
}
public void RemoveAgent(NavMeshAgent agent)
{
var tr = agent.transform;
var index = agents.IndexOf(agent);
agents.RemoveAt(index);
agentsTransforms.Remove(tr);
agentsPositions.RemoveAt(index);
if (useSpatialHash)
spatialHash.Remove(agentsPositions);
}
void OnDrawGizmos()
{
if (!showDebugGizmos)
return;
for (var i = 0; i < agents.Count; i++)
Gizmos.DrawRay(agents[i].destination, Vector3.up);
}
}
}