- Home /
FPS drops after reloading game,FPS drop after reloading game
Hello , I am having FPS drops after restarting my game few times.On First try i get 60 fps and after i keep restarting the game it drops to 20-30.
I have a game with 2 Views in 1 Scene . Main View And Game View , I turn off main view when i press play button and turn On game view . After i finish the game I turn off Game View and Turn Main view Back on.
Initially i get 60 FPS Constant when i start the game but after 5-6 restarts FPS counts get lowered to 20-30. I have also tried reloading the scene when game ends but that also doesn't seem to work This happens only when i enable/disable bots in game
I have around 100 bots in Game View with script attached to them.I have attached bots script below so you can tell me my mistake
public class BotController : MonoBehaviour {
#region PUBLIC_VARS
public Vector3 targetPosition;
public float chaseAngle = 30;
public Transform carBody;
public Transform chassis;
public Transform[] wheels;
public BoxCollider botCollider;
public bool hasBotDied = false;
public int botScore = 0;
public int botRank = 0;
[HideInInspector]
public bool hasSetRank = false;
#endregion
#region PRIVATE_VARS
public float speed = 20f;
private float rotateSpeed = 150;
private float driftAngle = 50;
private float leanAngle = 5;
private int pointCollected;
public int totalPointsCollected;
public int kills;
public float timeInGame;
private float speedIncreaseTimer;
// Detecting obstacles
private float detectAngle = 10;
private float detectDistance = 15;
private float rayRightHitDistance;
private float rayLeftHitDistance;
private float rayForwardHitDistance;
private Transform originalTransform;
// Tag Reference
private string pointTag = "Points";
private string obstacleTag = "Obstacles";
private string playerTag = "Player";
private string botTag = "Bot";
private float randomTime;
private float randomSpeed;
private Vector3 targetPoint;
#endregion
#region UNITYCALLBACKS
void OnEnable()
{
chaseAngle = Random.Range(0, 30);
ResetBotValues();
StartCoroutine(IncreaseSpeed());
}
private void OnDisable()
{
StopAllCoroutines();
}
void Update()
{
PlayerMovement();
timeInGame += Time.deltaTime;
botScore = (int)timeInGame + (kills * 2) + totalPointsCollected;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag.Equals(pointTag))
{
totalPointsCollected++;
speed -= Random.Range(0.2f, 0.5f);
}
else if (other.gameObject.tag.Equals(obstacleTag))
{
BotDead();
}
else if (other.gameObject.tag.Equals(playerTag))
{
if (other.gameObject.GetComponent<PlayerController>().speed < speed)
{
kills++;
}
}
else if (other.gameObject.tag.Equals(botTag))
{
if (other.gameObject.GetComponent<BotController>().speed > speed)
{
BotDead();
}
else
{
kills++;
}
}
}
#endregion
#region PUBLIC_FUNCTIONS
#endregion
#region PRIVATE_FUNCTIONS
private void PlayerMovement()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed, Space.Self);
RaycastHit hit;
rayRightHitDistance = 0f;
rayLeftHitDistance = 0f;
rayForwardHitDistance = 0f;
Collider[] hitColliders = Physics.OverlapSphere(transform.position, 8f);
if(hitColliders.Length > 0)
{
if (Physics.Raycast(transform.position, (transform.forward * 3) + transform.right, out hit, detectDistance + 10))
{
rayRightHitDistance = hit.distance;
}
else if (Physics.Raycast(transform.position, (transform.forward * 3) - transform.right, out hit, detectDistance + 10)) // Otherwise, if we detect an obstacle on our left side, swerve to the right
{
rayLeftHitDistance = hit.distance;
}
else
{
if (Vector3.Angle(transform.forward, targetPosition - transform.position) > chaseAngle)
{
Rotate(ChaseAngle(transform.forward, targetPosition - transform.position, Vector3.up));
}
else // Otherwise, stop rotating
{
Rotate(0);
}
}
if (Physics.Raycast(transform.position, transform.forward, out hit, detectDistance + 5))
{
rayForwardHitDistance = hit.distance;
//Rotate(1);
}
if (rayForwardHitDistance != 0)
{
if (rayRightHitDistance == 0 && rayLeftHitDistance == 0)
{
Rotate(1);
}
else if (rayRightHitDistance != 0 && rayLeftHitDistance == 0)
{
Rotate(-1);
}
else if (rayRightHitDistance == 0 && rayLeftHitDistance != 0)
{
Rotate(1);
}
}
else if (rayRightHitDistance != 0)
{
if (rayForwardHitDistance == 0 && rayLeftHitDistance == 0)
{
Rotate(0);
}
else if (rayForwardHitDistance != 0 && rayLeftHitDistance == 0)
{
Rotate(-1);
}
else if (rayForwardHitDistance == 0 && rayLeftHitDistance != 0)
{
Rotate(0);
}
}
else if (rayLeftHitDistance != 0)
{
if (rayForwardHitDistance == 0 && rayRightHitDistance == 0)
{
Rotate(0);
}
else if (rayForwardHitDistance != 0 && rayRightHitDistance == 0)
{
Rotate(1);
}
else if (rayForwardHitDistance == 0 && rayRightHitDistance != 0)
{
Rotate(0);
}
}
}
else
{
if (Vector3.Angle(transform.forward, targetPosition - transform.position) > chaseAngle)
{
Rotate(ChaseAngle(transform.forward, targetPosition - transform.position, Vector3.up));
}
else // Otherwise, stop rotating
{
Rotate(0);
}
}
}
public float ChaseAngle(Vector3 forward, Vector3 targetDirection, Vector3 up)
{
// Calculate the approach angle
float approachAngle = Vector3.Dot(Vector3.Cross(up, forward), targetDirection);
Vector3 relativePos = transform.InverseTransformDirection(targetDirection);
// If the angle is higher than 0, we approach from the left ( so we must rotate right )
if (approachAngle > 0f)
{
return 1f;
}
else if (approachAngle < 0f) //Otherwise, if the angle is lower than 0, we approach from the right ( so we must rotate left )
{
return -1f;
}
else // Otherwise, we are within the angle range so we don't need to rotate
{
return 0f;
}
}
/// <summary>
/// Rotates the car either left or right, and applies the relevant lean and drift effects
/// </summary>
/// <param name="rotateDirection"></param>
public void Rotate(float rotateDirection)
{
// If the car is rotating either left or right, make it drift and lean in the direction its rotating
if (rotateDirection != 0)
{
// Rotate the car based on the control direction
transform.eulerAngles += Vector3.up * rotateDirection * rotateSpeed * Time.deltaTime;
// Make the base of the car drift based on the rotation angle
carBody.localEulerAngles = new Vector3(0, Mathf.LerpAngle(carBody.localEulerAngles.y, rotateDirection * driftAngle + Mathf.Sin(Time.time * 50) * 50, Time.deltaTime), 0);// Mathf.LerpAngle(thisTransform.Find("Base").localEulerAngles.y, rotateDirection * driftAngle, Time.deltaTime);
// Make the chassis lean to the sides based on the rotation angle
if (chassis) chassis.localEulerAngles = Vector3.forward * Mathf.LerpAngle(chassis.localEulerAngles.z, rotateDirection * leanAngle, Time.deltaTime);// Mathf.LerpAngle(thisTransform.Find("Base").localEulerAngles.y, rotateDirection * driftAngle, Time.deltaTime);
// Play the skidding animation. In this animation you can trigger all kinds of effects such as dust, skid marks, etc
//GetComponent<Animator>().Play("Skid");
// Go through all the wheels making them spin, and make the front wheels turn sideways based on rotation
for (int index = 0; index < wheels.Length; index++)
{
// Turn the front wheels sideways based on rotation
if (index < 2) wheels[index].localEulerAngles = Vector3.up * Mathf.LerpAngle(wheels[index].localEulerAngles.y, rotateDirection * driftAngle, Time.deltaTime * 10);
// Spin the wheel
wheels[index].Rotate(Vector3.right * Time.deltaTime * speed * 20, Space.Self);
}
}
else // Otherwise, if we are no longer rotating, straighten up the car and front wheels
{
// Return the base of the car to its 0 angle
carBody.localEulerAngles = Vector3.up * Mathf.LerpAngle(carBody.localEulerAngles.y, 0, Time.deltaTime * 5);
// Return the chassis to its 0 angle
if (chassis) chassis.localEulerAngles = Vector3.forward * Mathf.LerpAngle(chassis.localEulerAngles.z, 0, Time.deltaTime * 5);// Mathf.LerpAngle(thisTransform.Find("Base").localEulerAngles.y, rotateDirection * driftAngle, Time.deltaTime);
// Play the move animation. In this animation we stop any previously triggered effects such as dust, skid marks, etc
//GetComponent<Animator>().Play("Move");
// Go through all the wheels making them spin faster than when turning, and return the front wheels to their 0 angle
for (int index = 0; index < wheels.Length; index++)
{
// Return the front wheels to their 0 angle
if (index < 2) wheels[index].localEulerAngles = Vector3.up * Mathf.LerpAngle(wheels[index].localEulerAngles.y, 0, Time.deltaTime * 5);
// Spin the wheel faster
wheels[index].Rotate(Vector3.right * Time.deltaTime * speed * 30, Space.Self);
}
}
}
public void BotDead()
{
botCollider.enabled = false;
gameObject.SetActive(false);
ResetBotValues();
RespawnBot();
}
private void ResetBotValues()
{
randomTime = Random.Range(0f, 100f);
randomSpeed = Random.Range(15f, 50f);
timeInGame = randomTime;
speed = randomSpeed;
kills = 0;
totalPointsCollected = 0;
botScore = 0;
targetPoint.x = Random.Range(-400f, 400f);
targetPoint.y = 0.5f;
targetPoint.z = Random.Range(-400f, 400f);
targetPosition = targetPoint;
}
private void RespawnBot()
{
int randomSpawnPointsIndex = Random.Range(0, 100);
//transform.position = GameManager.Instance.spawnPoints[randomSpawnPointsIndex].position;
gameObject.SetActive(true);
}
#endregion
#region CO-ROUTINES
private IEnumerator IncreaseSpeed()
{
while (true)
{
yield return new WaitForSeconds(3f);
speed += 1;
}
}
#endregion
In BotDead method after calling SetActive(false) are ResetBotValues and RespawnBot called? I assume that your problem is a result of coroutines. Are you sure that you are properly stopping the coroutine? Have you tried running the game 5-6 times with commented out coroutines (just for test purpose)?
Aren't you enabling anywhere botCollider?
@Sonky108 Temporary i have removed collision from the game to check the issue . As for the coroutine you can see i have Called StopAllCoroutines() in OnDisable() Function
Yes, I see that. $$anonymous$$y thought is to test the game without starting and stopping coroutine at all.