Hi, I’m definitely new to this and feel like I must be causing a dramatic frame rate drop with the following script:
using UnityEngine;
using System.Collections;
public class Enemy1Controller : MonoBehaviour
{
GameObject player;
public static float speed = 0.07f;
Vector3 playerPosition;
bool waitForCrowd;
float waitTimer;
float waitForCrowdCoolDown;
public CircleCollider2D enemyProximityTrigger;
void Start()
{
waitForCrowd = false;
waitForCrowdCoolDown = 3;
waitTimer = 0.5f;
player = GameObject.Find("Player");
}
void Update()
{
CrowdCoolDown();
Movement();
}
public void Movement()
{
if (waitForCrowd)
{
FightCrowd();
}
else
{
//Follows player
playerPosition = player.transform.position;
transform.position = Vector3.MoveTowards(transform.position, playerPosition, speed);
}
}
public void FightCrowd()
{
//Pauses after trigger and cool down then bounces in a random direction
waitTimer -= Time.deltaTime;
if (waitTimer < 0)
{
int setRandDir = (int)Random.Range(0, 3);
switch (setRandDir)
{
case 0:
transform.Translate(0.2f, 0.0f, 0.0f);
Debug.Log("Left");
break;
case 1:
transform.Translate(0.0f, 0.2f, 0.0f);
Debug.Log("Up");
break;
case 2:
transform.Translate(-0.2f, 0.0f, 0.0f);
Debug.Log("Right");
break;
case 3:
transform.Translate(0.0f, -0.2f, 0.0f);
Debug.Log("Down");
break;
}
waitTimer = 0.5f;
waitForCrowdCoolDown = 3;
waitForCrowd = false;
}
}
public void CrowdCoolDown()
{
waitForCrowdCoolDown -= Time.deltaTime;
Debug.Log(waitForCrowdCoolDown);
}
void OnTriggerStay2D(Collider2D other)
{
Debug.Log("Triggered");
if (waitForCrowdCoolDown <= 0)
{
//Reduces frequency of crowd interaction and not all react at the same time
if (Random.Range(1f, 10f) < 2) //Are too many of these at once to much to process?
waitForCrowd = true;
}
Debug.Log(waitForCrowd);
}
}
What happens here is many these “Enemy1” prefabs are Instatiated by a timer in another script. When they collide with each other while chasing the player, they have a chance of pausing for a moment and getting bumped around. When too many collisions are happening at once, the frame rate can plummet to single digits.
It is just a bunch of circle sprites chasing a square. It seems like when 4 to 6 of the circles are touching each other, that’s when the frame rate drops to unbearable levels.
I was thinking the culprit might be the RNG being called so often but if anyone can point me in a better direct to get these results I would really appreciate it.
The short answer is "No." When I remove the random numbers and set those variables, it still runs just as slow. The only thing that prevents the frame rate drop is disabling the triggers. What seems to help somewhat is changing OnTriggerStay2D to OnTriggerEnter2D. The trigger doesn't go off as often and I get similar results but performance still drops dramatically.
– CannJRBIf anything is called to much except for methods, floats and bools i usually get a huge framerate drop
– ALAC3I want to do it with scripts.It would be very good if you show me how to do it.
– PersianKiller