- Home /
Lag Caused by Overlapping 2D Colliders
I don't know whether this is a problem but from the articles i have seen elsewhere and all the solutions i have tried this seems to be the only source of the problem.. Iam making a 2D shooting game in which enemies are coming to attacks player from left and right side.. At first i was instantiating enemies and bullets and destroying them by comparing tags..
For example this script was on my bullet
public class
{
public void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("enemy1"))
{
Destroy(this.gameObject);
}
if (col.CompareTag("enemy2"))
{
Destroy(this.gameObject);
}
}
}
however the lag caused was insane and i read that instantiating and destroying objects multiple times can cause that.. i then reverted to POOLING and placed all my prefabs in the script using POOLING method.. still it wasn't to great effect.. the only source to my problem which i can deduce from this is that multiple 2D polygon colliders overlapping each other are causing this lag.. i have tried attaching rigidbody 2D and AreaEffector 2D with it and making my rigidBody Kinematic but to no avail.. If there are even 20,30 enemies in my scene but there are not colliding with each other the game runs smoothly. But if there are only 5,6 but their colliders overlap then they cause insane lag.. Any person who have experienced a problem like this before or who can provide an alternate, this would be a massive help for me..
Also since iam using only two enemies and making multiple copies of them the tags are also being reused which i have assigned to them which are "enemy1" and "enemy2" respectively. Does this also cause problem since unity may be unable to track the proper tag if 10,12 enemies are using the same tag ??
Answer by Ali_Dev3D · Dec 14, 2017 at 12:31 PM
If you are putting colliders on the whole body of the enemies then they are bound to overlap each other.. since your game is a 2D one i think what is happening is that when 2 or more enemies are overlapping each other their colliders are overlapping and this is causing the lag.. what i would suggest is remove the complete collider and only place it on the particular area where you want to hit the bullet..
also please ensure that the pooling algorithm which you are using is being called at correct time.. hopefully this would solve your problem.