/
BoidBehaviour.cs
executable file
·137 lines (107 loc) · 2.97 KB
/
BoidBehaviour.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
using UnityEngine;
using System.Collections;
using UnitySteer;
using UnitySteer.Vehicles;
public class BoidBehaviour : VehicleBehaviour, IRadarReceiver {
Boid boid;
public LayerMask ObstacleLayer;
public bool MovesVertically = true;
public float separationRadius = 5;
public float separationAngle = 135;
public float separationWeight = 12;
public float alignmentRadius = 7.5f;
public float alignmentAngle = 45;
public float alignmentWeight = 8;
public float cohesionRadius = 9;
public float cohesionAngle = 99;
public float cohesionWeight = 8;
public float maxSpeed = 3f;
public float maxForce = 15f;
public float radius = 0.5f;
public float randomizeStart = 15f;
// Use this for initialization
void Start()
{
if( rigidbody == null )
{
// Debug.Log( "Boid: Transform" );
boid = new Boid(transform, 1.0f, MovesVertically);
}
else
{
// Debug.Log( "Boid: Rigidbody" );
boid = new Boid(rigidbody, MovesVertically);
}
// vehicle.randomizeHeadingOnXZPlane();
boid.separationRadius = separationRadius;
boid.SeparationDeg = separationAngle;
boid.separationWeight = separationWeight;
boid.alignmentRadius = alignmentRadius;
boid.AlignmentDeg = alignmentAngle;
boid.alignmentWeight = alignmentWeight;
boid.cohesionRadius = cohesionRadius;
boid.CohesionDeg = cohesionAngle;
boid.cohesionWeight = cohesionWeight;
boid.MaxSpeed = maxSpeed;
boid.MaxForce = maxForce;
boid.Radius = radius;
if (randomizeStart > 0)
boid.Randomize(randomizeStart);
}
public override Vehicle Vehicle
{
get
{
return boid;
}
}
// Update is called once per frame
void Update () {
boid.Update(Time.deltaTime);
}
public void OnRadarEnter( Collider other, Radar sender )
{
BoidBehaviour boidBehaviour;
Obstacle obstacle;
if( ( 1 << other.gameObject.layer & ObstacleLayer ) > 0 )
{
obstacle = SphericalObstacle.GetObstacle( other.gameObject );
if( obstacle != null )
{
boid.Obstacles.Add( obstacle );
}
}
else
{
boidBehaviour = other.GetComponent( typeof( BoidBehaviour ) ) as BoidBehaviour;
if( boidBehaviour != null )
{
boid.Neighbors.Add( boidBehaviour.Vehicle );
}
}
}
public void OnRadarExit( Collider other, Radar sender )
{
BoidBehaviour boidBehaviour;
Obstacle obstacle;
if( ( 1 << other.gameObject.layer & ObstacleLayer ) > 0 )
{
obstacle = SphericalObstacle.GetObstacle( other.gameObject );
if( obstacle != null )
{
boid.Obstacles.Remove( obstacle );
}
}
else
{
boidBehaviour = other.GetComponent( typeof( BoidBehaviour ) ) as BoidBehaviour;
if( boidBehaviour != null )
{
boid.Neighbors.Remove( boidBehaviour.Vehicle );
}
}
}
public void OnRadarStay( Collider other, Radar sender )
{
}
}