Skip to content

Commit 3af324a

Browse files
committed
basic obstacle avoidance
1 parent 0490035 commit 3af324a

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

Assets/Prefabs/Boid.prefab

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ MonoBehaviour:
5252
<Cohesion>k__BackingField: 2
5353
<Separation>k__BackingField: 3
5454
<Pathfinding>k__BackingField: 2
55+
<Avoidance>k__BackingField: 1
5556
<Speed>k__BackingField: 10
5657
<Flock>k__BackingField: {fileID: 0}
5758
<Cognitive>k__BackingField: 0.8

Assets/Scenes/Main + BoidTest.unity

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ GameObject:
346346
- component: {fileID: 491750420}
347347
- component: {fileID: 491750419}
348348
- component: {fileID: 491750418}
349-
m_Layer: 0
349+
m_Layer: 6
350350
m_Name: Terrain
351351
m_TagString: Untagged
352352
m_Icon: {fileID: 0}

Assets/Scripts/Boid/Boid.cs

+16-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public struct Multipliers {
1212
[field: SerializeField] public float Cohesion { get; set; }
1313
[field: SerializeField] public float Separation { get; set; }
1414
[field: SerializeField] public float Pathfinding { get; set; }
15+
[field: SerializeField] public float Avoidance { get; set; }
1516
}
1617

1718
public class Boid : MonoBehaviour {
@@ -25,7 +26,7 @@ public class Boid : MonoBehaviour {
2526
[field: SerializeField] public Transform Target { private get; set; }
2627

2728
private Vector3 PersonalBest { get; set; }
28-
29+
private Vector3 _avoidance;
2930
private Vector3 GlobalBest {
3031
get {
3132
var neighbors = _neighbours.Get();
@@ -107,18 +108,26 @@ private Vector3 GetAcceleration(List<Boid> neighbours) {
107108
return Separation(neighbours) * Multipliers.Separation +
108109
Alignment(neighbours) * Multipliers.Alignment +
109110
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+
}
111120
}
112121

113-
private Vector3 Pathfinding(List<Boid> neighbours) {
122+
private Vector3 Pathfinding() {
114123
var global = Social * (GlobalBest - transform.position);
115124
var personal = Cognitive * (PersonalBest - transform.position);
116125

117126
var pathfinding = personal + global;
118127

119128
pathfinding -= Velocity;
120129
pathfinding = pathfinding.normalized * Speed;
121-
pathfinding = Vector3.ClampMagnitude(pathfinding, 0.25f);
130+
pathfinding = Vector3.ClampMagnitude(pathfinding, .2f);
122131

123132
return pathfinding;
124133
}
@@ -136,7 +145,7 @@ private Vector3 Alignment(List<Boid> neighbours) {
136145
alignment /= neighbours.Count - 1;
137146
alignment = alignment.normalized * Speed;
138147
alignment -= Velocity;
139-
alignment = Vector3.ClampMagnitude(alignment, 0.25f);
148+
alignment = Vector3.ClampMagnitude(alignment, .2f);
140149
}
141150
return alignment;
142151
}
@@ -153,7 +162,7 @@ private Vector3 Cohesion(List<Boid> neighbours) {
153162
cohesion -= transform.position;
154163
cohesion = cohesion.normalized * Speed;
155164
cohesion -= Velocity;
156-
cohesion = Vector3.ClampMagnitude(cohesion, 0.25f);
165+
cohesion = Vector3.ClampMagnitude(cohesion, .2f);
157166
}
158167
return cohesion;
159168
}
@@ -171,7 +180,7 @@ private Vector3 Separation(List<Boid> neighbours) {
171180
separation /= neighbours.Count - 1;
172181
separation = separation.normalized * Speed;
173182
separation -= Velocity;
174-
separation = Vector3.ClampMagnitude(separation, 0.25f);
183+
separation = Vector3.ClampMagnitude(separation, .2f);
175184
}
176185
return separation;
177186
}

ProjectSettings/TagManager.asset

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ TagManager:
1212
- boid
1313
- Water
1414
- UI
15-
-
15+
- Terrain
1616
-
1717
-
1818
-

0 commit comments

Comments
 (0)