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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Question Detect if gameobject has empty space to spawn in

Discussion in 'Scripting' started by VeryGoodDeveloperISwear, Mar 18, 2023.

  1. VeryGoodDeveloperISwear

    VeryGoodDeveloperISwear

    Joined:
    Jan 7, 2023
    Posts:
    35
    I currently have a 2D gameobject going to a random location when the game starts, but I want it to check if it collides with anything before it spawns in, and if it does the gameobject will generate a new random location. How can I do this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemySpawn : MonoBehaviour
    6. {
    7.     private float startX;
    8.     private float startY;
    9.     private Transform trans;
    10.  
    11.     private void Start()
    12.     {
    13.         trans = GetComponent<Transform>();
    14.         startX = Random.Range(-8, 8);
    15.         startY = Random.Range(-4, 4);
    16.         trans.position = new Vector2(startX, startY);
    17.     }
    18. }
    19.  
    20.  
     
    Last edited: Mar 18, 2023
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    480
  3. VeryGoodDeveloperISwear

    VeryGoodDeveloperISwear

    Joined:
    Jan 7, 2023
    Posts:
    35
    Sorry, I forgot to mention that it is in 2d. Will Physics.CheckBox work fine? Also, what do the docs mean by halfextent, layermask, and queryTriggerInteraction? I wasn't able to make sense of their explanation
     
  4. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    480
    Either of them will work fine. 2D is just 3D from a single perspective. One will just check a circle around your chosen area, and the other a square.

    Halfextents is one half the size of the box in local space. Where the extents would be the "extents" of the bounds of the box, half extents are literally half of that. A layermask defines what layers are checked so that layers that don't need to be checked can be bypassed. queryTriggerInteraction specifies whether the check will check trigger colliders as well, or just colliders without triggers.

    https://docs.unity3d.com/ScriptReference/BoxcastCommand-halfExtents.html
    https://docs.unity3d.com/ScriptReference/LayerMask.html
    https://docs.unity3d.com/ScriptReference/QueryTriggerInteraction.html
     
  5. VeryGoodDeveloperISwear

    VeryGoodDeveloperISwear

    Joined:
    Jan 7, 2023
    Posts:
    35
    Thanks for explaining, this makes more sense now. I tried implement Physics.CheckBox but it didn't work, what can I do to fix it?

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EnemySpawn : MonoBehaviour
    4. {
    5.     private float startX;
    6.     private float startY;
    7.     private Transform trans;
    8.     private Vector3 pos;
    9.  
    10.     private void Start()
    11.     {
    12.         trans = GetComponent<Transform>();
    13.         startX = Random.Range(-8, 8);
    14.         startY = Random.Range(-4, 4);
    15.         trans.position = new Vector2(startX, startY);
    16.  
    17.         pos = trans.position;
    18.         IsColliding();
    19.  
    20.         if (IsColliding() == true)
    21.         {
    22.      
    23.             startX = Random.Range(-8, 8);
    24.             startY = Random.Range(-4, 4);
    25.             trans.position = new Vector2(startX, startY);
    26.             pos = trans.position;
    27.  
    28.         }
    29.     }
    30.  
    31.     private bool IsColliding()
    32.     {
    33.         if (Physics.CheckBox(pos, trans.localScale) == true)
    34.         {
    35.             return true;
    36.         }
    37.         else
    38.         {
    39.             return false;
    40.         }
    41.     }
    42. }
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    4,020
    If this is 2d, you should be using methods from the
    Physics2D
    class.
     
  7. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    480
  8. VeryGoodDeveloperISwear

    VeryGoodDeveloperISwear

    Joined:
    Jan 7, 2023
    Posts:
    35
    Thank you, changing Physics.CheckBox to Physics2D.OverlapBox and adding the correct arguments worked