- Home /
Detect 2D GameObjects when "Colliding" with each other without RB and collider.
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[i].transform.position += asteroidMovement[i] * Time.deltaTime;
}
}
If you can help me with anything that can make my game's performance better whether you know a more efficient way that Instantiating object in the scene or anything else, please comment. Anything would be helpful. :)))))))
Answer by Vega4Life · Dec 02, 2018 at 06:06 PM
Just an idea, but here is a bounding box script that compares the bounds of the spriteRenderers. Then you can tell if they collide without colliders. Just place it in a scene, add two sprites, then colllide them.
using UnityEngine;
public class BoundingTest : MonoBehaviour
{
[SerializeField] SpriteRenderer firstRenderer;
[SerializeField] SpriteRenderer secondRenderer;
void Update ()
{
Bounds firstBounds = firstRenderer.bounds;
Bounds SecondBounds = secondRenderer.bounds;
if (firstBounds.Intersects(SecondBounds))
{
Debug.Log("Collided");
}
}
}
Should objects have anything else except SpriteRenderer? (cuz it's not working)
Nope. I created two sprites in the scene. Added 2 smiley faces as the sprite to the sprite renderer. Then, placed the script above on an object, referenced the two sprites and it works.
If you are using your own sprites, in your scene click on your sprite, hit the rect tool (next to scaling) and see the bounds of your sprite.
Nevertheless, this does work, I just tested it with multiple random sprites that use no colliders or anything.
this method doesn't work in Runtime. :((( if you place them on top of each other before running and then run it, it says collided if it far away and hit play and drag it on top of it, it doesn't say anything...
Your answer
Follow this Question
Related Questions
unity2D colliders not working properly,,Unity2D Colliders don't seem to be working HELP PLEASE! 0 Answers
OverlapCircle or OverlapArea best performance? 1 Answer
Making an object collide but transparent 0 Answers
Unity 5 - enable/disable gameobjects or just renderer/colliders 2 Answers
Calling gameobject.transform vs. just calling transform directly - Performance negligible? 1 Answer