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();
}
}
}
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.