Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rominv13 · Jul 04, 2018 at 06:51 AM · aimemory-leakreloadingrestart gamememory leaks

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
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sonky108 · Jul 04, 2018 at 07:26 AM 0
Share

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?

avatar image rominv13 Sonky108 · Jul 04, 2018 at 08:42 AM 0
Share

@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

avatar image Sonky108 rominv13 · Jul 04, 2018 at 08:44 AM 0
Share

Yes, I see that. $$anonymous$$y thought is to test the game without starting and stopping coroutine at all.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

149 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a chance Unity profiler does not show memory leak but the exe/app actually leaks ? (Windows standalone) 1 Answer

Memory leak with DataAdapter.Fill to DataSet 0 Answers

Enemy AI Reloading Problem, they reload with queue 0 Answers

Question About Camera's, Canvases and Memory 0 Answers

Saving layout and enemy stats with Binary Formatter 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges