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): void OnTriggerStay(Collider other) { if (other.tag == "Enemy" ) { if (inTrigger && Time.time > lastFireTime) { lastFireTime = Time.time + delayTime; Fire(other.transform); } } else Physics.IgnoreCollision(collider, other.collider); } void Fire(Transform currentTarget) { GameObject shot = Instantiate(bullet, transform.position, Quaternion.identity) as GameObject; shot.GetComponent<BulletPhysics>().target = currentTarget; Destroy((shot.gameObject), 2); } 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
i commented out Code (CSharp): else Physics.IgnoreCollision(collider, other.collider); since onenter/onexit takes care of it anyway
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.
FYI, a lot of people assume box colliders are the better ones, but sphere colliders are faster than box colliders. Box colliders have to check intersections. http://forum.unity3d.com/threads/capsule-vs-box-colliders.34254/
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.
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
Just like the 'tag' field, but it's an integer instead of a string, so comparisons will be much faster. Layers GameObject.layer
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.