Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

lag after multiple instanced of a script

Discussion in 'Scripting' started by ghost123_1234, Mar 1, 2016.

  1. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    hello
    I have an object with this script on it:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Linq;
    4. using System.Collections.Generic;
    5. public class Soldier : MonoBehaviour
    6. {
    7.     public GameObject bulletPrefab;
    8.     public float Timer = 0;
    9.     public bool ishyding = false;
    10.     public bool goingtohideposition = false;
    11.     public bool goingback = false;
    12.     public Vector3 oldpos;
    13.     public GameObject hidepos;
    14.     private float speed = 0.03f;
    15.    
    16.     public GameObject oldposgameobject;
    17.     public float distancetohidepos;
    18.     public float distancetooldpos;
    19.     RaycastHit hit;
    20.  
    21.  
    22.     public float y;
    23.     public int howmanyobjectsincollider;
    24.  
    25.     public List<GameObject> objects1 = new List<GameObject>();
    26.  
    27.     void Start()
    28.     {
    29.         transform.parent.position = new Vector3(transform.parent.position.x, transform.parent.position.y - 0.55f, transform.parent.position.z + 0.3f);
    30.         objects1 = GameObject.FindGameObjectsWithTag("hideplaces").ToList();
    31.         // oldposgameobject.transform.position = this.transform.position;
    32.         oldpos = transform.parent.position;
    33.         FindTarget();
    34.  
    35.     }
    36.     void Update()
    37.     {
    38.  
    39.         if(Input.GetKey(KeyCode.R))
    40.         {
    41.             transform.parent.position = Vector3.MoveTowards(transform.position, hidepos.transform.position, speed);
    42.         }
    43.         y = transform.parent.eulerAngles.y;
    44.         transform.eulerAngles = new Vector3(0, 0, 0);
    45.         ShootTimer();
    46.         #region walk
    47.         if (goingtohideposition == true)
    48.         {
    49.  
    50.           distancetohidepos = Vector3.Distance(transform.position, hidepos.transform.position);
    51.  
    52.             if (hidepos != null)
    53.             {
    54.                 Debug.Log("0");
    55.                 transform.parent.position = Vector3.MoveTowards(transform.position, hidepos.transform.position, speed);
    56.                 //transform.LookAt(hidepos.transform.position);
    57.                 if (Physics.Raycast(transform.position, transform.forward, out hit, 2))
    58.                 {
    59.                     if (hit.collider.gameObject.tag == "Monster")
    60.                     {
    61.                         Debug.Log("1");
    62.                         speed = 0;
    63.                         Debug.DrawRay(transform.position, transform.forward, Color.red);
    64.                     }
    65.                     else
    66.                     {
    67.                         Debug.Log("2");
    68.                         speed = 0.03f;
    69.                     }
    70.                 }
    71.                 if (Physics.Raycast(transform.position, (transform.forward + transform.right * -.5f) * 1, out hit, 2))
    72.                 {
    73.                     if (hit.collider.gameObject.tag == "Monster")
    74.                     {
    75.                         Debug.Log("3");
    76.                         speed = 0;
    77.                         Debug.DrawRay(transform.position, (transform.forward + transform.right * -.5f) * 1, Color.red);
    78.                     }
    79.                     else
    80.                     {
    81.                         Debug.Log("4");
    82.                         speed = 0.03f;
    83.                     }
    84.                 }
    85.                 if (Physics.Raycast(transform.position, (transform.forward + transform.right * .5f) * 1, out hit, 2))
    86.                 {
    87.                     if (hit.collider.gameObject.tag == "Monster")
    88.                     {
    89.                         Debug.Log("5");
    90.                         speed = 0;
    91.                         Debug.DrawRay(transform.position, (transform.forward + transform.right * .5f) * 1, Color.red);
    92.                     }
    93.                     else
    94.                     {
    95.                         Debug.Log("6");
    96.                         speed = 0.03f;
    97.                     }
    98.                 }
    99.  
    100.                 if (distancetohidepos <= 0.1)
    101.                 {
    102.                     goingtohideposition = false;
    103.                     ishyding = true;
    104.                 }
    105.  
    106.             }
    107.         }
    108.         #endregion
    109.  
    110.         if (goingback == true)
    111.         {
    112.  
    113.             ishyding = false;
    114.             goingtohideposition = false;
    115.             //distancetooldpos = Vector3.Distance(transform.position, oldpos);
    116.             transform.parent.position = Vector3.MoveTowards(transform.position, oldpos, speed);
    117.             //transform.LookAt(oldpos);
    118.             if (Physics.Raycast(transform.position, transform.forward, out hit, 1))
    119.             {
    120.                 if (hit.collider.gameObject.tag == "Monster")
    121.                 {
    122.                     Debug.Log("1");
    123.                     speed = 0;
    124.                     Debug.DrawRay(transform.position, transform.forward, Color.red);
    125.                 }
    126.                 else
    127.                 {
    128.                     Debug.Log("2");
    129.                     speed = 0.03f;
    130.                 }
    131.             }
    132.             if (Physics.Raycast(transform.position, (transform.forward + transform.right * -.5f), out hit, 1))
    133.             {
    134.                 if (hit.collider.gameObject.tag == "Monster")
    135.                 {
    136.                     Debug.Log("3");
    137.  
    138.                     speed = 0;
    139.                     Debug.DrawRay(transform.position, (transform.forward + transform.right * -.5f), Color.red);
    140.                 }
    141.                 else
    142.                 {
    143.                     Debug.Log("4");
    144.                     speed = 0.03f;
    145.                 }
    146.             }
    147.             if (Physics.Raycast(transform.position, (transform.forward + transform.right * .5f), out hit, 1))
    148.             {
    149.                 if (hit.collider.gameObject.tag == "Monster")
    150.                 {
    151.                     Debug.Log("5");
    152.                     speed = 0;
    153.                     Debug.DrawRay(transform.position, (transform.forward + transform.right * .5f), Color.red);
    154.                 }
    155.                 else
    156.                 {
    157.                     Debug.Log("6");
    158.                     speed = 0.03f;
    159.                 }
    160.  
    161.             }
    162.            
    163.             if (distancetooldpos  < 0.1)
    164.             {
    165.                 Debug.Log("isgelijk");
    166.                 Debug.Log(goingback);
    167.                 goingback = false;
    168.             }
    169.         }
    170.  
    171.    
    172.        
    173.     }
    174.  
    175.  
    176.     public void ShootTimer()
    177.     {
    178.         if (Timer > 0)
    179.         {
    180.             Timer -= Time.deltaTime;
    181.         }
    182.     }
    183.  
    184.     void OnTriggerEnter(Collider col)
    185.     {
    186.         if (col.GetComponent<Monster>())
    187.         {
    188.             goingtohideposition = true;
    189.             howmanyobjectsincollider += 1;
    190.         }
    191.     }
    192.     void OnTriggerExit(Collider col)
    193.     {
    194.         if (col.GetComponent<Monster>())
    195.         {
    196.             howmanyobjectsincollider -= 1;
    197.         }
    198.         if (howmanyobjectsincollider <= 0)
    199.         {
    200.             Debug.Log("terug");
    201.             //goingback = true;
    202.             StartCoroutine(goingbacktrue());
    203.         }
    204.     }
    205.  
    206.  
    207.     IEnumerator goingbacktrue()
    208.     {
    209.         yield return new WaitForSeconds(5.0f);
    210.  
    211.         if (howmanyobjectsincollider <= 0)
    212.         {
    213.             goingback = true;
    214.         }
    215.     }
    216.  
    217.  
    218.  
    219.  
    220.  
    221.     void FindTarget()
    222.     {
    223.  
    224.         float lowestDist = Mathf.Infinity;
    225.  
    226.  
    227.         for (int i = 0; i < objects1.Count; i++)
    228.         {
    229.  
    230.             float dist = Vector3.Distance(objects1[i].transform.position, transform.position);
    231.  
    232.             if (dist < lowestDist)
    233.             {
    234.                 lowestDist = dist;
    235.                 hidepos = objects1[i];
    236.             }
    237.  
    238.         }
    239.     }
    240.  
    241.  
    242.     void OnTriggerStay(Collider col)
    243.     {
    244.         if (col.GetComponent<Monster>())
    245.         {
    246.             if (Timer <= 0)
    247.             {
    248.                 if (ishyding == false)
    249.                 {
    250.                     Debug.Log("shoot");
    251.                     GameObject g = (GameObject)Instantiate(bulletPrefab, transform.position, Quaternion.identity);
    252.                     g.GetComponent<Bullet>().target = col.transform;
    253.                     Timer = 2f;
    254.                 }
    255.             }
    256.         }
    257.  
    258.     }
    259. }
    260.  
    It works fine when i have 1 or 2 of those soldiers. But when i have 10 and the bool goingtohideposition = true and the soldiers start moving the game begins lagging like hell and i have 5 to 10 fps remanaining.

    When i press the R key wich you can see it can also move then it doesnt happen

    Hope someone can help me so the code works without lag
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    First of all try to remove or comment out the Debug.Logs. They're expensive and shouldn't be used when measuring performance.
    Second, perhaps your soldiers stay in a trigger all the time. People usually try to avoid OnTriggerStay and find workarounds, the simplest one being combinations of Enter and Exit.
    So if it's not only due to debugs, try to comment it out as well and report back.
     
  3. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    i removed those.
    Still fps drop of 20 fps but way better. but still not really good.
    Do i also need to remove debug.drawray in order to improve this?

    I also taught it could maybe be because of vector3.distance but i dont know if that gives you less performance?
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Worth a try. Just right now, without testing it, i can't tell how expensive it is.

    Also, you may want to use CompareTag (doesn't have to increase performance noticeably but according to other people it at least reduces garbage. Lot's of garbage can cause hiccups when it has to be collected).

    How long is your list of hiding places (the list named objects1 in your script)? If there are lot's of objects in it it may also be a bottleneck regarding performance.
    Last but not least, try to avoid frequent Instantiating (same goes for destroying) as you do in OnTriggerStay. If you need bullet objects, have a look into pooling systems which are a lot more efficient.