Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 SamR71 · Jul 13, 2015 at 11:13 AM · aiperformanceperformance optimizationrts

More than 20 instances of a certain script cause fps to skyrocket, is there something I'm doing wrong here?

Hi, I'm currently working on the AI for an RTS game I'm working on. One of the AI's is set to find the nearest target, move to it, then destroy it, then repeat. If it gets shot in this cycle however, it finds the nearest shooter, kills it, then continues to the original cycle. The AI works as intended, but not to any considerable scale. More than 20 of these AIs spawned results in a severe fps drop (not a problem for any other scripts, and I need to be able to handle up to 200. Here is the AI code, I'm not sure if there is any one issue, or if the whole script needs to be rewritten.

public class activeSoldierAI : MonoBehaviour { GameObject target; Target targettype; bool allclear = true; int hp = 50; int lastsavedhp; bool isShooting; Rigidbody selfBody; // Use this for initialization void Start () { targettype = Target.Building; target = ClosestTarget ("Building"); lastsavedhp = hp; StartCoroutine (shoot()); selfBody = GetComponent ();

     }
     
     // Update is called once per frame
     void Update () {
         Debug.Log (hp);
         Debug.Log (allclear);
         //Debug.Log (targettype);
         Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
         Debug.DrawRay(new Vector3(transform.position.x,transform.position.y + 1.5f,transform.position.z), transform.forward, Color.green, 15000);
         if(targettype == Target.Building){
             if(!Physics.Raycast(new Vector3(transform.position.x,transform.position.y + 1.5f,transform.position.z), transform.forward, 25) && target != null){
                 transform.LookAt (new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z));
                 rigidbody.AddForce (transform.forward *  7 * 60 * Time.deltaTime);
                 isShooting = false;
                 selfBody.drag = 0;
             }else if(target != null){
                 isShooting = true;
                 selfBody.drag = 6;
             }else{
                 target = ClosestTarget("Building");
                 //Debug.Log("HOwde doo");
             }                      
         }
         if (hp < lastsavedhp && allclear == true) {
             targettype = Target.Soldier;
             target = ClosestTarget ("reserveSoldier");
             allclear = false;
         }
         if(targettype == Target.Soldier){
             if(target != null){
                 Debug.Log("YUR FUCKED MATE");
                 isShooting = true;
                 transform.LookAt (new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z));
                 selfBody.drag = 6;
             }else{
                 targettype = Target.Building;
                 target = ClosestTarget("Building");
                 isShooting = false;
                 allclear = true;
                 lastsavedhp = hp;
                 Debug.Log("HOwde doo");
             }                      
         }
         if (hp <= 0) {
             Destroy(gameObject);
         }
     }
 
     IEnumerator shoot() {
         while (true) {
             if(isShooting == true){
                 if(targettype == Target.Building){
                     target.GetComponent<buildinghealth>().TakeDamage();
                     Debug.Log("Blammo!");
                 }else{
                     target.GetComponent<reserveSoldierAI>().TakeDamage();
                     Debug.Log("Blammo!");
                 }
                 //Debug.Log("Blammo!");
             }
             yield return new WaitForSeconds(1f);
         }
     }
     public void TakeDamage(){
         hp -= 10;
     }
 
     GameObject ClosestTarget(string tag){
         GameObject[] objectsWithTag = GameObject.FindGameObjectsWithTag (tag);
         GameObject closestObject = null;
         foreach (GameObject obj in objectsWithTag) {
             if (!closestObject) {
                 closestObject = obj;
             }
             if (Vector3.Distance (transform.position, obj.transform.position) <= Vector3.Distance (transform.position, closestObject.transform.position)) {
                 closestObject = obj;
             }
         }
         return closestObject;
     }
 }
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 Lahzar · Jul 13, 2015 at 02:45 PM 0
Share

First of all remove all debugging tools! These lag the game alot! Also ins$$anonymous$$d of having everything in your update function you should try to use threads and coroutines with a quick timer. Also use return statements where necessary. If this doesnt work youre gonna have to come up with something much more clever and efficient I am afraid :/

Everything inside the update function must be completed before the next frame can start rendering, but with coroutines the game can render several frames without having to stop, atleast if you use yields correctly!

avatar image Dave-Carlile · Jul 13, 2015 at 03:38 PM 0
Share

The biggest problem is probably those Debug.Log calls that run every frame. Those would be a big problem when running in the editor.

avatar image Owen-Reynolds · Jul 13, 2015 at 05:09 PM 0
Share

The more common way to write your Q is "script runs too slow."It's like buying a $25 cup of coffee. Once, not a huge deal. 20 times a week, it adds up. But the root cause is the script/coffee costing too much.

For example, there are are few Qs here, I think, about faster ways to find the nearest enemy.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Calling gameobject.transform vs. just calling transform directly - Performance negligible? 1 Answer

Figuring out which specific objects are slow to render? 0 Answers

How to implement Jobs?,How can I increase performance with Jobs? 0 Answers

Problem with the performance when activating gameObject 0 Answers

Low Poly Alpha Cutout vs Full Models Performance 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