-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlockingBehaviour.cs
More file actions
205 lines (180 loc) · 6.25 KB
/
FlockingBehaviour.cs
File metadata and controls
205 lines (180 loc) · 6.25 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlockingBehaviour : MonoBehaviour
{
public Vector3 position;
public Vector3 velocity;
public Vector3 acceleration;
public Vector3 destination;
private float velocityMagnitude = 20f;
private float perceptionRange = 15f;
private float seperationRange = 10f;
public float maxForce;
public float maxForceSeperation;
public float maxForceAlign;
public float maxVelocity;
private bool order = false;
private bool run = false;
private SelectionUtil selectUtil;
void Start()
{
position = GetComponent<Transform>().position;
velocity = new Vector3();
acceleration = new Vector3();
GameObject go = GameObject.FindGameObjectWithTag("GameController");
selectUtil = go.GetComponent<SelectionController>().selectionUtil;
}
public void setDestinationVecloity(Vector3 dest)
{
velocity = dest * 10;
}
// Update is called once per frame
void Update()
{
//transform.position += velocity * velocityMagnitude * Time.deltaTime;
randomize();
if (run)
{
// cohesion();
// seperation();
//align();
}
if(order){
seekPath();
}
velocity += acceleration;
transform.position += velocity * Time.deltaTime;
acceleration *= 0;
}
public void align()
{
Vector3 desiredVelocity = new Vector3();
int total = 0;
foreach (var unit in selectUtil.selectedTable.Values)
{
var dist = Vector3.Distance(unit.transform.position, transform.position);
if (unit.GetInstanceID() != GetInstanceID() && dist < perceptionRange)
{
desiredVelocity += unit.GetComponent<FlockingBehaviour>().velocity;
total++;
}
}
if (total > 0)
{
// Craig Reynolds steering formula Steering = desired velocity - current velocity
desiredVelocity /= total;
// setting magnitude to max velocity
desiredVelocity.Normalize();
desiredVelocity *= maxVelocity;
desiredVelocity -= velocity;
Vector3.ClampMagnitude(desiredVelocity, maxForceAlign);
}
acceleration += desiredVelocity;
}
public void cohesion()
{
Vector3 averageLocation = new Vector3();
int total = 0;
// THIS WILL STOP WORKING IOCNE sELECTED BECAUSE WE ITERATE THRU SELECTED OBJECTS TO AVERAGE SHIT
// Keep copy until a move order is finished
foreach (var unit in selectUtil.selectedTable.Values)
{
var dist = Vector3.Distance(unit.transform.position, transform.position);
if (unit.GetInstanceID() != GetInstanceID() && dist < perceptionRange)
{
averageLocation += unit.transform.position;
total++;
}
}
if (total > 0)
{
// Craig Reynolds steering formula Steering = desired velocity - current velocity
averageLocation /= total;
// Vector that points in average direction
averageLocation -= transform.position;
averageLocation.Normalize();
averageLocation *= maxVelocity;
averageLocation -= velocity;
Vector3.ClampMagnitude(averageLocation, maxForce);
}
acceleration += averageLocation;
}
public void seperation()
{
Vector3 averageLocation = new Vector3();
int total = 0;
// THIS WILL STOP WORKING once SELECTED BECAUSE WE ITERATE THRU SELECTED OBJECTS TO AVERAGE SHIT
// Keep copy until a move order is finished
foreach (var unit in selectUtil.selectedTable.Values)
{
float dist = Vector3.Distance(unit.transform.position, transform.position);
if (unit.GetInstanceID() != GetInstanceID() && dist < seperationRange)
{
// create vector pointing at other units, it scales with inverse proportions
// average them and thats where we need to move to get seperated
Vector3 diff = transform.position - unit.transform.position;
averageLocation += diff;
total++;
}
}
if (total > 0)
{
averageLocation /= total;
averageLocation.Normalize();
averageLocation *= maxVelocity;
averageLocation -= velocity;
Vector3.ClampMagnitude(averageLocation, maxForceSeperation);
}
acceleration += averageLocation;
}
public float maxSeekSpeed;
public float maxSeekForce;
private Vector3[] route;
private int routeCounter;
private bool reachedNode = false;
private void seekPath(){
if (route!=null) {
if(routeCounter >= route.Length){
order = false;
routeCounter = 0;
return;
}
seek(route[routeCounter]);
}
}
private void seek(Vector3 location)
{
if(Vector3.Distance(location, transform.position)<1){
velocity = new Vector3();
reachedNode = true;
routeCounter++;
return;
}
Vector3 desiredVelocity = location - transform.position;
desiredVelocity.Normalize();
desiredVelocity*=maxSeekSpeed;
Vector3 steering = desiredVelocity - velocity;
Vector3.ClampMagnitude(steering, maxSeekForce);
acceleration += steering;
}
public void giveOrder(Vector3[] route,bool recieved) {
order = true;
this.route = route;
}
private void randomize()
{
if (Input.GetKey(KeyCode.Keypad0) && GetComponent<Selected>().selected)
{
if (!run)
{
Vector2 randomVector = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f));
randomVector.Normalize();
velocityMagnitude = Random.Range(3f, 7f);
velocity.x = randomVector.x * velocityMagnitude;
velocity.z = randomVector.y * velocityMagnitude;
}
run = true;
}
}
}