Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 DkSker · Mar 15, 2021 at 06:38 PM · lagprofilerenemy aitrackingoptimize

Optimizing script to improve lag

Currently, my game will spawn in a few enemies into the scene when a "wave" starts. This does not cause any lag, however, on each of the enemy, I have a script that tracks every single structures that belongs to the player (These structures are stored in an empty object so I used a foreach loop to get each structure's position). After getting each of the position, the enemy will find the nearest structure to go to. This itself is obvious that it will cause some lag, so I added a small timer to control the interval between finding a new nearest target (1 - 2 seconds). This helps with the frame drop issue as it is not tracking on every single frame. However, taking a look at my profiler, at each tracking moments, it has a huge lag spike. When playing the game, you can feel that the enemies are all trying to track and find the nearest object to go to (As the game stops for a brief moment, normally around 0.1 seconds). Is there a way I can optimize this? I have some solution in mind though, as I am using a for loop to instantiate all of the enemies in the scene at once, I'm pretty sure this will cause that future tracking "moments" will happen for all of the enemies since they are spawned in at the same time. So would spawning them in at different times help. Are there any other better way to improve the code?

 private void track_items()
     {
         small_timer -= Time.deltaTime;
         if (small_timer <= 0)
         {
             foreach (Transform structure in mc_structures.transform)
             {
                 tracked_items.Add(structure);
             }
             foreach (Transform o in tracked_items)
             {
                 float dist = Vector2.Distance(o.position, transform.position);
                 if (o.name.Equals("main_character"))
                 {
                     dist /= 2;
                 }
                 if (dist < distance)
                 {
                     distance = dist;
                     target = o.gameObject;
                 }
             }
             small_timer = small_time;
         }
 }
 

small_time is set to 2 seconds. Also, I added a if statement to make the enemies more "interested" in the player. mc_structure is the empty game objects that all of the structures will be stored in. Thanks for the help.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by CodesCove · Mar 15, 2021 at 08:11 PM

Some ideas for optimization:

I usually add a small random time to timers when doing some mass operations that should be distributed in temporal axis evenly. This at least prevents certain spikes.

I also cache all the needed object references (so that you need to only find them once or if any changes are made) and preferably have a static List where each object registers it own reference on a Awake and remove it's own reference in OnDestroy. Then you have always up-to-date list of references that you can iterate in a quite fast way and access it anywhere in you code.

When calculating distances it's good to assess why it's done. If the distance is only needed for comparison the you don't need to call the quite expensive Distance method. You can use Vector3.sqrMagnitude value to do a fast comparison between two squared vector length. https://docs.unity3d.com/ScriptReference/Vector3-sqrMagnitude.html

If you have really larger amount of calculations to be done and your target platform is suitable then consider to move the calculations from CPU to GPU via ComputeShader.. This is not so trivial and lot's of considerations need to be done, but might be interesting option, at lease to get to know this option, if you have not programmed shaders before. https://docs.unity3d.com/Manual/class-ComputeShader.html

Comment
Add comment · Show 1 · Share
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 DkSker · Mar 16, 2021 at 11:54 AM 0
Share

Thank you for your responses, I did try to cache all of the positions of my structures into one place and only update the list whenever a new structure is instantiated into the scene, so all of the enemies would reference to that script. I can't really say for sure what helped, I also added a small random time roughly from 1 to 3 seconds for the enemy to check nearest object again. This way not all the enemies are checking the distance at the same time, so the task is spread out.

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

117 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

Related Questions

How to reduce Draw-Calls? 1 Answer

Extreme lag in game 1 Answer

Editor incredibly slow, weird profiler output. 1 Answer

What is a dirty scene objects? 2 Answers

good guidelines for iOS Profiler 1 Answer


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