- Home /
Unity freezes on play mode
Hey everyone! I've been trying to build a flocking simulation with fishes and one of my friends helped me to fix part of it and it worked very well on her computer, however, when I tried it on mine it kept freezing everytime I pressed play.
Some people told me it could be an infinite loop, but I'm not sure if that's the problem.
Here is my scripts:
Boid.cs
//using System; using System.Collections; using System.Collections.Generic; using System.Dynamic; using UnityEngine;
public class Boid : MonoBehaviour {
private static Boid _instance = null;
private static object _lock = new object();
public static Boid Instance {
get
{
lock (_lock)
{
if (_instance != null) return _instance;
Boid boid = FindObjectOfType<Boid>();
if (boid == null)
{
GameObject go = new GameObject("[Boid]", typeof(Boid));
boid = go.GetComponent<Boid>();
}
_instance = boid;
}
return _instance;
}
}
public GameObject fishPrefab;
public GameObject goalPrefab;
public float goalRange = 1f;
public int tankSize = 10;
//generate number of fishes
static int numFish = 10;
private GameObject[] _allFish = new GameObject[numFish];
public GameObject[] allFish
{
get { return _allFish; }
}
//public static Vector3 goalPos = Vector3.zero;
public Vector3 goalPos
{
get
{
if(goalPrefab != null) return goalPrefab.transform.position;
return Vector3.zero;
}
}
// Start is called before the first frame update
void Start()
{
//create loop interating through the numFish array
for (int i = 0; i < numFish; i++)
{
//create a postion to our fishes (in a 3Dimensional space)
allFish[i] = (GameObject)Instantiate(fishPrefab,
Random.insideUnitSphere * tankSize,
Quaternion.identity);
}
}
// Update is called once per frame
void Update()
{
//Reset where the goal position is randomly
foreach(var fish in allFish)
{
if((fish.transform.position - goalPos).magnitude <= goalRange)
{
goalPrefab.transform.position = Random.insideUnitSphere * tankSize * 0.75f;
break;
}
}
}
}
Flock.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Flock : MonoBehaviour { public float speed = 1f; float rotationSpeed = 4.0f; Vector3 averageHeading; Vector3 averagePosition; //value of the neighbor distance float neighbourDistance = 3.0f; float minDistance = 1f;
bool turning = false;
// Start is called before the first frame update
void Start()
{
//generate a small difference in each neighbor's speed
speed = Random.Range(0.5f, 1);
}
// Update is called once per frame
void Update()
{
if (Vector3.Distance(transform.position, Vector3.zero) >= Boid.Instance.tankSize)
turning = true;
else
turning = false;
if (turning)
{
Vector3 direction = Vector3.zero - transform.position;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
speed = Random.Range(0.9f, 1f);
}
else
{
//flock value
if (Time.frameCount % 5 == 0)
ApplyRules(); //function called every 5 times
}
transform.Translate(0, 0, Time.deltaTime * speed);
}
void ApplyRules()
{
GameObject[] allFish = Boid.Instance.allFish;
// Cohesion of boids
Vector3 vcentre = Vector3.zero; //center of group
Vector3 vavoid = Vector3.zero; //points away from any neighbors
//group speed
float gSpeed = 0.1f;
//goal position
Vector3 goalPos = Boid.Instance.goalPos;
float dist; //distance variable
//calculate group size
int groupSize = 0;
foreach (GameObject fish in allFish)
{
if (fish != this.gameObject)
{
dist = Vector3.Distance(fish.transform.position, this.transform.position);
if (dist <= neighbourDistance)
{
vcentre += fish.transform.position;
groupSize++;
if (dist < minDistance)
{
vavoid = vavoid + (this.transform.position - fish.transform.position);
}
//average speed of group by adding the speed of the flock
Flock anotherFlock = fish.GetComponent<Flock>();
gSpeed = gSpeed + anotherFlock.speed;
}
}
}
if (groupSize > 0)
{
//calculate average centre and average speed
vcentre = vcentre / (float)groupSize + (goalPos - this.transform.position);
speed = gSpeed / (float)groupSize;
Vector3 direction = (vcentre + vavoid) - transform.position;
if (direction != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
}
}
}
}
Your answer
Follow this Question
Related Questions
Terrain tools error, can anyone help? 0 Answers
How to check that only one key is pressed 2 Answers
Combine Mesh Instance is null/Editor freezing when using SkinnedCloth component 0 Answers
unity 3d freezes 5 Answers
I am having an error in my script 1 Answer