Hi guys,
I’m making a game Where at Start 25,000 Asteroid are spawned using an ObjectPooler and they start moving at random direction to infinite. If 2 asteroids collide both should be destroyed. If I use RigidBody2D for each object and use OnTriggerEnter2D or OnCollisionEnter2D methods, my game’s performance drops dramatically, I don’t need physics at no point. I want the most efficient and fastest way of detecting 2 objects “colliding”.
Here is my ObjectPooler Script:
public static ObjectPooler instance;
[System.Serializable]
public class Pool
{
public int size;
public GameObject prefab;
}
public List<Pool> pools;
public Dictionary<string, Queue<GameObject>> poolDictionary;
void Start () {
instance = this;
poolDictionary = new Dictionary<string, Queue<GameObject>>();
foreach(Pool pool in pools)
{
Queue<GameObject> objectPool = new Queue<GameObject>();
for (int i = 0; i < pool.size; i++)
{
GameObject obj = Instantiate(pool.prefab);
obj.SetActive(false);
// I dont want to use Rigidbody2d or collider2d
obj.AddComponent<CircleCollider2D>().isTrigger = true;
obj.AddComponent<AsteroidMovement>();
obj.AddComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
objectPool.Enqueue(obj);
}
poolDictionary.Add(pool.tag, objectPool);
}
}
public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation)
{
if(!poolDictionary.ContainsKey(tag)) return null;
GameObject objToSpawn = poolDictionary[tag].Dequeue();
objToSpawn.transform.position = position;
objToSpawn.transform.rotation = rotation;
objToSpawn.SetActive(true);
return objToSpawn;
}
My GameController Script:
private void SpawnAsteroids() {
for (Generating X Position) . . . .
{ //Spawning asteroids like in a square form in scene like this -> . . . .
for (Generating Y Position) . . . .
{
GameObject newObj = objectPooler.SpawnFromPool("asteroid", Position, Rotation);
asteroidsTransform.Add(newObj.transform); // I use this for moving the objects
}
}
}
void Update () {
//For Moving the Asteroids after they are spawned
if(asteroidSpawned)
for (int i = 0; i < asteroidsTransform.Count; i++)
{
asteroidsTransform<em>.transform.position += asteroidMovement _* Time.deltaTime;_</em>
* }*
}