@@ -12,6 +12,7 @@ public struct Multipliers {
12
12
[ field: SerializeField ] public float Cohesion { get ; set ; }
13
13
[ field: SerializeField ] public float Separation { get ; set ; }
14
14
[ field: SerializeField ] public float Pathfinding { get ; set ; }
15
+ [ field: SerializeField ] public float Avoidance { get ; set ; }
15
16
}
16
17
17
18
public class Boid : MonoBehaviour {
@@ -25,7 +26,7 @@ public class Boid : MonoBehaviour {
25
26
[ field: SerializeField ] public Transform Target { private get ; set ; }
26
27
27
28
private Vector3 PersonalBest { get ; set ; }
28
-
29
+ private Vector3 _avoidance ;
29
30
private Vector3 GlobalBest {
30
31
get {
31
32
var neighbors = _neighbours . Get ( ) ;
@@ -107,18 +108,26 @@ private Vector3 GetAcceleration(List<Boid> neighbours) {
107
108
return Separation ( neighbours ) * Multipliers . Separation +
108
109
Alignment ( neighbours ) * Multipliers . Alignment +
109
110
Cohesion ( neighbours ) * Multipliers . Cohesion +
110
- Pathfinding ( neighbours ) * Multipliers . Pathfinding ;
111
+ Pathfinding ( ) * Multipliers . Pathfinding +
112
+ _avoidance * Multipliers . Avoidance ;
113
+ }
114
+
115
+ private void FixedUpdate ( ) {
116
+ _avoidance = Vector3 . zero ;
117
+ if ( Physics . Raycast ( new Ray ( transform . position , Velocity . normalized ) , out var hit , perception , ~ LayerMask . NameToLayer ( "Terrain" ) ) ) {
118
+ _avoidance = hit . normal ;
119
+ }
111
120
}
112
121
113
- private Vector3 Pathfinding ( List < Boid > neighbours ) {
122
+ private Vector3 Pathfinding ( ) {
114
123
var global = Social * ( GlobalBest - transform . position ) ;
115
124
var personal = Cognitive * ( PersonalBest - transform . position ) ;
116
125
117
126
var pathfinding = personal + global ;
118
127
119
128
pathfinding -= Velocity ;
120
129
pathfinding = pathfinding . normalized * Speed ;
121
- pathfinding = Vector3 . ClampMagnitude ( pathfinding , 0.25f ) ;
130
+ pathfinding = Vector3 . ClampMagnitude ( pathfinding , .2f ) ;
122
131
123
132
return pathfinding ;
124
133
}
@@ -136,7 +145,7 @@ private Vector3 Alignment(List<Boid> neighbours) {
136
145
alignment /= neighbours . Count - 1 ;
137
146
alignment = alignment . normalized * Speed ;
138
147
alignment -= Velocity ;
139
- alignment = Vector3 . ClampMagnitude ( alignment , 0.25f ) ;
148
+ alignment = Vector3 . ClampMagnitude ( alignment , .2f ) ;
140
149
}
141
150
return alignment ;
142
151
}
@@ -153,7 +162,7 @@ private Vector3 Cohesion(List<Boid> neighbours) {
153
162
cohesion -= transform . position ;
154
163
cohesion = cohesion . normalized * Speed ;
155
164
cohesion -= Velocity ;
156
- cohesion = Vector3 . ClampMagnitude ( cohesion , 0.25f ) ;
165
+ cohesion = Vector3 . ClampMagnitude ( cohesion , .2f ) ;
157
166
}
158
167
return cohesion ;
159
168
}
@@ -171,7 +180,7 @@ private Vector3 Separation(List<Boid> neighbours) {
171
180
separation /= neighbours . Count - 1 ;
172
181
separation = separation . normalized * Speed ;
173
182
separation -= Velocity ;
174
- separation = Vector3 . ClampMagnitude ( separation , 0.25f ) ;
183
+ separation = Vector3 . ClampMagnitude ( separation , .2f ) ;
175
184
}
176
185
return separation ;
177
186
}
0 commit comments