Prevent Instantiation within Player (Unity)

I am working with random instantiation but i don’t want it to instantiate inside my player as they move. How would I achieve this? The only thing I could find was Physics2D.OverlapCircle but I don’t know how to implement it.

  
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class DeleteAndSpawn : MonoBehaviour
{
    int _score = 0;
    public GameObject Collectable;
    UIManager UiManagerVar;
    //float TimerforCollectable = 0;
    // Start is called before the first frame update
    void Start()
    {
        
     
    }
 
    // Update is called once per frame
    void Update()
    {
        UiManagerVar = GameObject.Find("Canvas").GetComponent<UIManager>();
        Collectable = GameObject.FindGameObjectWithTag("Finish");
        //timerforCollectionFunc();
        Debug.Log(TimerforCollectable);
        
    }
 
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
 
        {
            
            Instantiate(Collectable, new Vector3(Random.Range(-4, 4), Random.Range(-4, 4), 0), Quaternion.identity);
            Destroy(this.gameObject, 0);
            
            UiManagerVar.UpdateofNumberEnemy();
            // if player has entered the collision zone, prevent other collision detection
        }
 
       else if (collision.tag == "PlayerMove")
        {Instantiate(Collectable, new Vector3(Random.Range(-4, 4), Random.Range(-4, 4), 0), Quaternion.identity);
           Destroy(this.gameObject, 0);
            
           UiManagerVar.UpdateofNumber();      
 
        }
        
    }
    /*public void timerforCollectionFunc()
    {
        TimerforCollectable += Time.deltaTime;
    }*/
 
 
}

Hi Mikhail,

You know the player position, and you probably also know the position where you want to spawn your things. Then you could compare both. And if the target position is too close to the player, you could generate a new position.

Vector3.Distance could also be interesting for you.


See also:

when you generate new position that position has a potential of overlapping as well. I tried what you said but I still get overlapping.

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Enemy")
        {
            Vector2 randomCollectPosition = new Vector2(Random.Range(-4.5f, 4), Random.Range(-4.5f, 4));
            Vector2 randomCollectPosition4 = new Vector2(Random.Range(-4f, 4.2f), Random.Range(-4.5f, 4));
            if (Vector2.Distance(randomCollectPosition, Enemy.transform.position) > 1)
            {
                Instantiate(Collectable, randomCollectPosition, Quaternion.identity);
            }
            else
            {
                Instantiate(Collectable, randomCollectPosition4, Quaternion.identity);
            }
            Destroy(gameObject);
            UiManagerVar.UpdateofNumberEnemy();
        }

        else if (collision.tag == "Player")
        {
            Vector2 randomCollectPosition2 = new Vector2(Random.Range(-4, 4), Random.Range(-4, 4));
            Vector2 randomCollectPosition3 = new Vector2(Random.Range(-4.5f, 4.5f), Random.Range(-4, 4));
            //if collider is equal to he circle dont instantiate
            if (Vector2.Distance(randomCollectPosition2, PlayerOne.transform.position) > 1)
            {
                Instantiate(Collectable, randomCollectPosition2, Quaternion.identity);
            }
            else
            {
                Instantiate(Collectable, randomCollectPosition3, Quaternion.identity);
            }
            Destroy(gameObject);
            UiManagerVar.UpdateofNumber();
        }   

Define “overlapping”. Once you did that, it should be possible to write a condition that prevents “overlapping”.

1 Like

I found this code that prevents overlapping.

public class DeleteAndSpawn : MonoBehaviour
{
    
    public GameObject Collectable;
   // Vector2 randomCollectPosition;
    //public float ScanRadius = 1;
    //public LayerMask exclude;
    public GameObject PlayerOne;
    //public LayerMask whatIsPlayer;
    public GameObject EnemyDestroy;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            Vector2 randomCollectPosition2 = new Vector2(Random.Range(-4, 4), Random.Range(-4, 4));
          while (Vector2.Distance(randomCollectPosition2, PlayerOne.transform.position) <= 7)
            
          {
          randomCollectPosition2 = new Vector2(Random.Range(-10, 10), Random.Range(-10, 10));
          
          }
            Instantiate(Collectable, randomCollectPosition2, Quaternion.identity);
            Debug.Log(Vector2.Distance(randomCollectPosition2, PlayerOne.transform.position));
            Destroy(gameObject);
            


        }
        else if(collision.tag == "Enemy")
        {
            Vector2 randomCollectPosition2 = new Vector2(Random.Range(-4, 4), Random.Range(-4, 4));
            while (Vector2.Distance(randomCollectPosition2, EnemyDestroy.transform.position) <= 7)

            {
                randomCollectPosition2 = new Vector2(Random.Range(-10, 10), Random.Range(-10, 10));

            }
            Instantiate(Collectable, randomCollectPosition2, Quaternion.identity);
            Debug.Log(Vector2.Distance(randomCollectPosition2, EnemyDestroy.transform.position));
            Destroy(gameObject);
        }
    }
}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms