Skip to content

Commit

Permalink
Fixed boids not working when game is built
Browse files Browse the repository at this point in the history
  • Loading branch information
Calneideck committed Jun 17, 2017
1 parent 9411580 commit 8c0a0fd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 82 deletions.
72 changes: 6 additions & 66 deletions Assets/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1681,8 +1681,11 @@ MonoBehaviour:
_wanderDist: 2.5
_wanderRadius: 1
_wanderJitter: 5
spawnPos: {fileID: 1108646925}
spawnAmount: 30
spawns:
- spawnPos: {fileID: 1108646925}
amount: 30
- spawnPos: {fileID: 829397814}
amount: 30
spawnRadius: 2
boidPrefab: {fileID: 1588338096451542, guid: 6d0c914735719b34987eeb5828acbc2c, type: 2}
colours:
Expand Down Expand Up @@ -1813,7 +1816,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 14
m_RootOrder: 13
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &840948183
GameObject:
Expand Down Expand Up @@ -3259,69 +3262,6 @@ Transform:
m_PrefabParentObject: {fileID: 4835754588201566, guid: e00bfbd7ec58ea344b39fd5e451c5cb2,
type: 2}
m_PrefabInternal: {fileID: 1280925029}
--- !u!1 &1295365262
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 1295365264}
- component: {fileID: 1295365263}
m_Layer: 0
m_Name: BoidController 2
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1295365263
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1295365262}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5e45505cf4fa2ba459065c6835605a82, type: 3}
m_Name:
m_EditorClassIdentifier:
player: {fileID: 1506875869}
_neighbourRange: 2
_maxSpeed: 1.5
_wander: 1
_cohesion: 1.3
_alignment: 0.2
_separation: 0.97
_wallAvoidance: 1.5
_wanderDist: 2.5
_wanderRadius: 1
_wanderJitter: 5
spawnPos: {fileID: 829397814}
spawnAmount: 30
spawnRadius: 2
boidPrefab: {fileID: 1588338096451542, guid: 6d0c914735719b34987eeb5828acbc2c, type: 2}
colours:
- {r: 1, g: 0, b: 0, a: 1}
- {r: 1, g: 0, b: 1, a: 1}
- {r: 0, g: 0, b: 1, a: 1}
- {r: 0, g: 1, b: 1, a: 1}
- {r: 0, g: 1, b: 0, a: 1}
- {r: 0.9862069, g: 1, b: 0, a: 1}
- {r: 1, g: 0.50980395, b: 0, a: 1}
--- !u!4 &1295365264
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1295365262}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 13
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1319203760
Prefab:
m_ObjectHideFlags: 0
Expand Down
7 changes: 5 additions & 2 deletions Assets/Scripts/Boid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private enum BoidState { FLOCKING, SEARCHING, ENGAGING };
public static List<Boid> Boids = new List<Boid>();
public static Group LargestGroup = new Group(0, null);

void Start()
void Awake()
{
Boids.Add(this);
}
Expand Down Expand Up @@ -88,7 +88,8 @@ void Flocking()
awayTime += Time.deltaTime;
if (awayTime >= 2)
{
Pathfinding.instance.RequestPath(gameObject, LargestGroup.boid.transform.position);
if (Pathfinding.instance && LargestGroup.boid)
Pathfinding.instance.RequestPath(gameObject, LargestGroup.boid.transform.position);
state = BoidState.SEARCHING;
}
}
Expand Down Expand Up @@ -118,8 +119,10 @@ void GetNeighbours()
neighbours.Clear();
foreach (Boid boid in Boids)
if (boid != this)
{
if ((boid.transform.position - transform.position).sqrMagnitude < Mathf.Pow(BoidController.neighbourRange, 2))
neighbours.Add(boid);
}

if (LargestGroup.boid == this || neighbours.Count > LargestGroup.count)
{
Expand Down
51 changes: 37 additions & 14 deletions Assets/Scripts/BoidController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@

public class BoidController : MonoBehaviour
{
[System.Serializable()]
public struct Spawn
{
public Transform spawnPos;
public int amount;

public Spawn(Transform spawnPos, int amount)
{
this.spawnPos = spawnPos;
this.amount = amount;
}
}

public Player player;
[SerializeField]
private float _neighbourRange = 4;
Expand Down Expand Up @@ -40,31 +53,41 @@ public class BoidController : MonoBehaviour
public static float wanderJitter;

[Header("Spawning")]
public Transform spawnPos;
public int spawnAmount = 20;
public Spawn[] spawns;
public float spawnRadius = 1.5f;
public GameObject boidPrefab;
public Color[] colours;

void Start()
{
for (int i = 0; i < spawnAmount; i++)
{
Vector3 pos = spawnPos.position + Random.insideUnitSphere * spawnRadius;
pos.y = Random.Range(0.1f, 0.7f);
GameObject boid = GameObject.Instantiate(boidPrefab, pos, Random.rotationUniform, transform);
boid.GetComponentInChildren<TrailRenderer>().startColor = colours[Random.Range(0, colours.Length)];
boid.GetComponentInChildren<TrailRenderer>().endColor = colours[Random.Range(0, colours.Length)];
boid.GetComponent<Boid>().Setup(player);
}
foreach (Spawn spawn in spawns)
for (int i = 0; i < spawn.amount; i++)
{
Vector3 pos = spawn.spawnPos.position + Random.insideUnitSphere * spawnRadius;
pos.y = Random.Range(0.1f, 0.7f);
GameObject boid = GameObject.Instantiate(boidPrefab, pos, Random.rotationUniform, transform);
boid.GetComponentInChildren<TrailRenderer>().startColor = colours[Random.Range(0, colours.Length)];
boid.GetComponentInChildren<TrailRenderer>().endColor = colours[Random.Range(0, colours.Length)];
boid.GetComponent<Boid>().Setup(player);
}
}

private void OnDrawGizmos()
void OnValidate()
{
Gizmos.DrawWireSphere(spawnPos.position, neighbourRange);
neighbourRange = _neighbourRange;
maxSpeed = _maxSpeed;
wander = _wander;
cohesion = _cohesion;
alignment = _alignment;
separation = _separation;
wallAvoidance = _wallAvoidance;

wanderDist = _wanderDist;
wanderRadius = _wanderRadius;
wanderJitter = _wanderJitter;
}

void OnValidate()
void Awake()
{
neighbourRange = _neighbourRange;
maxSpeed = _maxSpeed;
Expand Down

0 comments on commit 8c0a0fd

Please sign in to comment.