Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by milad440550 · Dec 02, 2018 at 05:50 PM · collisiongameobjectperformancephysics2dperformance optimization

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;
         }
  }


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image milad440550 · Dec 02, 2018 at 05:54 PM 0
Share

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. :)))))))

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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");
         }
     }
 }



Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image milad440550 · Dec 02, 2018 at 06:27 PM 0
Share

Should objects have anything else except SpriteRenderer? (cuz it's not working)

avatar image Vega4Life milad440550 · Dec 02, 2018 at 06:35 PM 0
Share

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.

avatar image milad440550 Vega4Life · Dec 02, 2018 at 06:39 PM 0
Share

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...

Show more comments
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

196 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges