- Home /
Ant Colony Frame Rate Optimization
Hi. I have a script that runs an algorithm that simulates the behavior of an ant or many ants. The when the ant isnt standing on a quad that is called a "square" it creates one using Instantiate and adds it to the array of squares. As the ant moves very quickly, it creates lots and lots of squares. Up to 28,059 squares can occur and as I add in more ants or let one ant run for a while, the game becomes very slow due to the large number of squares in the scene. I am looking for a new way to do this so that the frame rate is increased and I an add more ants with ease. The only two requirements for a square is that it needs to be read as either black or brown and needs to be able to be changed between the two colors. I was thinking about using one texture and then editing the pixels on it. But I read that Texture2D.Apply() is not very fast. What would you guys recommend for obtaining the fastest read, edit, and storage of all these squares? Here is the code:
#pragma strict
var moveRate : float;
var queenRate : float;
var antLifeTime : float;
var step : int;
var queenAnt : GameObject;
var builderAnt : GameObject;
var black : GameObject;
var brown : GameObject;
var blackMat : Material;
var brownMat : Material;
var squareParent : GameObject;
var squares : GameObject[];
var ants : GameObject[];
var newAnt : GameObject;
var rot : Quaternion;
rot.eulerAngles = new Vector3(0, 0, 0);
function Start () {
InvokeRepeating("Move", 0.1f, moveRate);
InvokeRepeating("Fall", 0.1f, 0.15);
InvokeRepeating("QueenLayAnt", 0.1f, queenRate);
}
function Update () {
squares = GameObject.FindGameObjectsWithTag("Square");
ants = GameObject.FindGameObjectsWithTag("Ant");
if(Input.GetMouseButtonDown(0)){
var position : Vector2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
position = new Vector2(Mathf.Floor(position.x), Mathf.Floor(position.y));
position = new Vector2(position.x + 0.5, position.y + 0.5);
if(Input.GetKey(KeyCode.LeftShift)){
newAnt = Instantiate(queenAnt, new Vector3(position.x, position.y, -1.5), rot);
newAnt.transform.name = "Queen Ant";
} else {
if(Input.GetKey(KeyCode.LeftControl)){
newAnt = Instantiate(builderAnt, position, rot);
newAnt.transform.name = "Burried Ant";
Destroy(newAnt, antLifeTime);
} else {
newAnt = Instantiate(builderAnt, position, rot);
newAnt.transform.name = "Builder Ant";
Destroy(newAnt, antLifeTime);
}
}
}
}
function Fall(){
for(var i : int = 0; i < ants.length; i++){
if(ants[i].transform.position.y > 62.5){
ants[i].transform.position.y -= 1;
}
}
}
function QueenLayAnt(){
for(var i : int = 0; i < ants.length; i++){
if(ants[i].transform.name == "Queen Ant"){
newAnt = Instantiate(builderAnt, new Vector3(ants[i].transform.position.x, ants[i].transform.position.y, 0), rot);
newAnt.transform.name = "Builder Ant";
Destroy(newAnt, antLifeTime);
}
}
}
function Move(){
step += 1;
for(var i : int = 0; i < ants.length; i++){
var currentSquare : GameObject;
for(var o : int = 0; o < squares.length; o++){
if(squares[o].transform.position == ants[i].transform.position){
currentSquare = squares[o];
}
}
if(currentSquare == null && ants[i].transform.position.y < 63.5){
if(ants[i].transform.name == "Builder Ant"){
currentSquare = Instantiate(brown, ants[i].transform.position, rot);
currentSquare.transform.parent = squareParent.transform;
}
} // 199x141
if(currentSquare !== null){
if(ants[i].transform.name == "Builder Ant" || ants[i].transform.name == "Burried Ant"){
if(currentSquare.transform.name == "Brown" || currentSquare.transform.name == "Brown(Clone)"){
if(ants[i].transform.rotation.eulerAngles.z == 0){
if(ants[i].transform.position.x < 99.5){
ants[i].transform.position.x += 1;
ants[i].transform.rotation.eulerAngles.z = 270;
} else {
ants[i].transform.position.x -= Random.Range(0, 3);
ants[i].transform.rotation.eulerAngles.z = 270;
}
} else {
if(ants[i].transform.rotation.eulerAngles.z == 270){
if(ants[i].transform.position.y > -78.5){
ants[i].transform.position.y -= 1;
ants[i].transform.rotation.eulerAngles.z = 180;
} else {
ants[i].transform.position.y += Random.Range(0, 3);
ants[i].transform.rotation.eulerAngles.z = 180;
}
} else {
if(ants[i].transform.rotation.eulerAngles.z == 90){
if(ants[i].transform.position.y < 62.5){
ants[i].transform.position.y += 1;
ants[i].transform.rotation.eulerAngles.z = 0;
} else {
ants[i].transform.position.y -= Random.Range(0, 3);
ants[i].transform.rotation.eulerAngles.z = 0;
}
} else {
if(ants[i].transform.position.x > -99.5){
ants[i].transform.position.x -= 1;
ants[i].transform.rotation.eulerAngles.z = 90;
} else {
ants[i].transform.position.x += Random.Range(0, 3);
ants[i].transform.rotation.eulerAngles.z = 90;
}
}
}
}
currentSquare.transform.name = "Black";
currentSquare.GetComponent(Renderer).material = blackMat;
ants[i].transform.name = "Builder Ant";
} else {
if(currentSquare.transform.name == "Black" || currentSquare.transform.name == "Black(Clone)"){
if(ants[i].transform.rotation.eulerAngles.z == 0){
if(ants[i].transform.position.x > -99.5){
ants[i].transform.position.x -= 1;
ants[i].transform.rotation.eulerAngles.z = 90;
} else {
ants[i].transform.position.x += Random.Range(0, 3);
ants[i].transform.rotation.eulerAngles.z = 90;
}
} else {
if(ants[i].transform.rotation.eulerAngles.z == 270){
if(ants[i].transform.position.y < 62.5){
ants[i].transform.position.y += 1;
ants[i].transform.rotation.eulerAngles.z = 0;
} else {
ants[i].transform.position.y -= Random.Range(0, 3);
ants[i].transform.rotation.eulerAngles.z = 0;
}
} else {
if(ants[i].transform.rotation.eulerAngles.z == 90){
if(ants[i].transform.position.y > -78.5){
ants[i].transform.position.y -= 1;
ants[i].transform.rotation.eulerAngles.z = 180;
} else {
ants[i].transform.position.y += Random.Range(0, 3);
ants[i].transform.rotation.eulerAngles.z = 180;
}
} else {
if(ants[i].transform.position.x < 99.5){
ants[i].transform.position.x += 1;
ants[i].transform.rotation.eulerAngles.z = 270;
} else {
ants[i].transform.position.x -= Random.Range(0, 3);
ants[i].transform.rotation.eulerAngles.z = 270;
}
}
}
}
currentSquare.transform.name = "Brown";
currentSquare.GetComponent(Renderer).material = brownMat;
ants[i].transform.name = "Builder Ant";
}
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Unlimited Blood on Floor 1 Answer
Why changing eulerangles causes so much FPS drop? 0 Answers
Texture2D.Apply Optimization or Alternative 1 Answer
Error : BCE0024 1 Answer