Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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
0
Question by unity_E790889B07041C2772A3 · Mar 18 at 12:27 AM · physics2dif-statementsphysics.overlapsphere

Prevent overlapping of spawned object over AI and Player

A collectible is being instantiated every time a player or AI overlaps the collectible, but sometimes the player and AI are above each other and the collectible is instantiated twice. So, I'm wandering how would I prevent the collectible from being instantiated over the player or the AI. I know about Physics2D.OverlapCircle but I don't know how to implement it. Here's my spawn code:

 public class DeleteAndSpawn : MonoBehaviour
 {
     
     public GameObject Collectable;
     UIManager UiManagerVar;
     Vector2 randomCollectPosition;
     Collider2D radius;
     //public float ScanRadius;
     public void Update()
     {
        
         UiManagerVar = GameObject.Find("Canvas").GetComponent<UIManager>();
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.tag == "Enemy")
         {
             Vector2 randomCollectPosition = new Vector2(Random.Range(-4, 4), Random.Range(-4, 4));
             Instantiate(Collectable, randomCollectPosition, Quaternion.identity);
             Destroy(gameObject);
             UiManagerVar.UpdateofNumberEnemy();
         }
 
         else if (collision.tag == "Player")
         {
             Vector2 randomCollectPosition2 = new Vector2(Random.Range(-4, 4), Random.Range(-4, 4));
             //if collider is equal to he circle dont instantiate
             Instantiate(Collectable, randomCollectPosition2, Quaternion.identity);
            /*Collider2D radius = Physics2D.OverlapCircle(transform.position, ScanRadius, exclude);
             if (radius != null)
               {
                 Destroy(gameObject);
                 }*/
             Destroy(gameObject);
             UiManagerVar.UpdateofNumber();
         }   
     }
 }







Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Master109 · Mar 18 at 02:14 AM

The performant way would be:

 using UnityEngine;
 
 public class DeleteAndSpawn : MonoBehaviour
 {
     public GameObject Collectable;
     public float ScanRadius;
     UIManager UiManagerVar;
     Vector2 randomCollectPosition;
     Collider2D radius;
 
     void Update ()
     {
         UiManagerVar = GameObject.Find("Canvas").GetComponent<UIManager>();
     }
 
     void OnTriggerEnter2D (Collider2D collision)
     {
         if (collision.tag == "Enemy")
         {
             Vector2 randomCollectPosition = new Vector2(Random.Range(-4, 4), Random.Range(-4, 4));
             Instantiate(Collectable, randomCollectPosition, Quaternion.identity);
             Destroy(gameObject);
             UiManagerVar.UpdateofNumberEnemy();
         }
 
         else if (collision.tag == "Player")
         {
             Transform playerTrs = collision.transform;
             Vector2 playerPosition = playerTrs.position;
             Vector2 randomCollectPosition2;
             do
             {
                 randomCollectPosition2 = new Vector2(Random.Range(-4, 4), Random.Range(-4, 4));
             } while ((randomCollectPosition2 - playerPosition).sqrMagnitude <= ScanRadius * ScanRadius);
             // } while (Vector2.Distance(randomCollectPosition2, playerPosition) <= ScanRadius);
             Instantiate(Collectable, randomCollectPosition2, Quaternion.identity);
             Destroy(gameObject);
             UiManagerVar.UpdateofNumber();
         }   
     }
 }

Keep in mind that this way doesn't take into account the collision shape of the player and just uses the player's position. Using Physics2D.OverlapCircle the answer would be:

 using UnityEngine;
 
 public class DeleteAndSpawn : MonoBehaviour
 {
     public GameObject Collectable;
     public float ScanRadius;
     UIManager UiManagerVar;
     Vector2 randomCollectPosition;
     Collider2D radius;
 
     void Update ()
     {
         UiManagerVar = GameObject.Find("Canvas").GetComponent<UIManager>();
     }
 
     void OnTriggerEnter2D (Collider2D collision)
     {
         if (collision.tag == "Enemy")
         {
             Vector2 randomCollectPosition = new Vector2(Random.Range(-4, 4), Random.Range(-4, 4));
             Instantiate(Collectable, randomCollectPosition, Quaternion.identity);
             Destroy(gameObject);
             UiManagerVar.UpdateofNumberEnemy();
         }
 
         else if (collision.tag == "Player")
         {
             Vector2 randomCollectPosition2;
             LayerMask whatIsPlayer = LayerMask.GetMask(LayerMask.LayerToName(collision.gameObject.layer));
             do
             {
                 randomCollectPosition2 = new Vector2(Random.Range(-4, 4), Random.Range(-4, 4));
             } while (Physics2D.OverlapCircle(randomCollectPosition2, ScanRadius, whatIsPlayer) != null);
             Instantiate(Collectable, randomCollectPosition2, Quaternion.identity);
             Destroy(gameObject);
             UiManagerVar.UpdateofNumber();
         }   
     }
 }

Also, you might already know these last two notes but in case it helps: Random.Range(-4, 4) generates a random integer between -4 and 3 (it can also be equal to those -4 or 3). The maximum value is 3 because the second parameter is "exclusive". Also, Random.Range(-4f, 4f) will generate a random float between -4 and 4.

Comment
Add comment · 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

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

136 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

Related Questions

Check a boolean function without using Update() 1 Answer

2D version of Physics.OverlapSphere? 2 Answers

Collision functions without physical collisions 1 Answer

Detecting top most UI element using Collider2D 0 Answers

Alternates to Linecast for a gameobject without collider. 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