Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Instantiate - overlapping / check collision when instantiated

Discussion in 'Scripting' started by frozendog_bren, Dec 9, 2015.

  1. frozendog_bren

    frozendog_bren

    Joined:
    Nov 29, 2014
    Posts:
    19
    The game should spawn static "enemies" randomly in a limited area, but the instantiated objects overlap each other quite often. My idea was to fix that by detecting collision and then just giving them a new random location not so far from the origin. Unfortunately, it doesn't work and i can't find out why.

    Notes:

    - "Enemy_Tx" is the tag used for three different types of enemies.
    - Every object in scene should stay at 0 on y position, so on the grid.
    - The link to an earlier version of the prototype in development: http://brenooshiro.itch.io/4th - Check it out and give me some feedback! :)

    Thanks!

    Here's the script​

    Code (CSharp):
    1.     void OnCollisionEnter (Collision collision)
    2.     {
    3.         if(collision.gameObject.tag == "Enemy_T1" || collision.gameObject.tag == "Enemy_T2" || collision.gameObject.tag == "Enemy_T3")
    4.         {
    5.             collision.gameObject.transform.position = new Vector3(transform.position.x + Random.Range(-10f, 10f), 0f, transform.position.z + Random.Range(-10f, 10f));
    6.             print("collision worked");
    7.         }
    8.     }
     
    Last edited: Dec 9, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. frozendog_bren

    frozendog_bren

    Joined:
    Nov 29, 2014
    Posts:
    19