Search Unity

Optimize HUNDREDS of objects

Discussion in 'Scripting' started by Ziron999, Feb 4, 2015.

  1. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
    How can i optimize hundreds of objects firing at each other? I've already got box colliders because they are the least memory usage by a long shot. everything is fine until i add this part:

    Code (CSharp):
    1.   void OnTriggerStay(Collider other)
    2.      {
    3.        if (other.tag == "Enemy" )
    4.        {
    5.          if (inTrigger && Time.time > lastFireTime)
    6.          {
    7.            lastFireTime = Time.time + delayTime;
    8.            Fire(other.transform);
    9.          }
    10.        }
    11.        else
    12.          Physics.IgnoreCollision(collider, other.collider);
    13.      }
    14.   void Fire(Transform currentTarget)
    15.      {
    16.        GameObject shot = Instantiate(bullet, transform.position, Quaternion.identity) as GameObject;
    17.        shot.GetComponent<BulletPhysics>().target = currentTarget;
    18.        Destroy((shot.gameObject), 2);
    19.      }
    basically i think the slowdown is coming from the way i'm doing my delay is there a better way? The profiler states 80k calls is being done in stay in a matter of seconds
     
  2. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
    i commented out
    Code (CSharp):
    1.        else
    2.          Physics.IgnoreCollision(collider, other.collider);
    since onenter/onexit takes care of it anyway
     
  3. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    It's better to use raycasts for bullet simulation than actual bullets - that's hundreds of high speed physics objects being simulated. Also, if you have to create bullets, use object pooling. Unity generates a lot of overhead for instantiating an object so instead recycle already instantiate objects with SetActive(false). When you need another bullet, if there's an inactive bullet in the pool, use SetActive(true) and continue to do what you will with it.
     
  4. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
  5. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
  6. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    In any case a lot of the problems are with:

    OnTriggerStay
    - this is expensive as it's called all the time non stop.

    if(other.tag=="Enemy")
    - this is expensive as it's a string compare. Use layer compare.

    GameObject shot = Instantiate(bullet, transform.position, Quaternion.identity) as GameObject;
    - this is very expensive, pool your bullets instead.

    Destroy((shot.gameObject), 2);
    - this is very expensive, won't be needed with pooling

    shot.GetComponent<BulletPhysics>().target = currentTarget;
    - get component is very expensive, use a different coding pattern.


    In short, your code is an exercise in how to make your game as slow and inefficient as possible, so if it profiles as being the performance bottleneck then look at those issues. Doing the above is fine if its once every so often but if its happening all the time, it needs to be fixed.
     
    image28 likes this.
  8. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Beat me to it.... Pooling is awesome

     
  9. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
  10. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
    No longer use triggerstay used a boolean check in enter/exit instead.

    Not sure how to use layer compare but i have setup layers for enemies/buildings/detection/ect. already. if you are talking about physics.ignorecollisionlayer i've already set that up in the editor. guess i'm not entirely sure how this works.

    switched shots into a list with object pooling

    so far this has been a huge performance boost
     
  11. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Just like the 'tag' field, but it's an integer instead of a string, so comparisons will be much faster.

    Layers
    GameObject.layer
     
  12. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
    aww so it should be other.gameobject.layer instead of just other.layer that's where my problem i was trying to figure this out.
     
    Last edited: Feb 5, 2015