Search Unity

Question Prevent spawn overlapping with Transform?

Discussion in 'Scripting' started by Mr_Packer12, Jan 29, 2023.

  1. Mr_Packer12

    Mr_Packer12

    Joined:
    Aug 11, 2022
    Posts:
    30
    I have a script here that spawns X amount of objects when called upon. The code is written with Transform, not Vector3 and spawns the object randomly in an array of transform points. I am trying to prevent spawn overlapping. I understand the function .CheckSphere, however that is for Vector3s and will not work in this instance. What is the equivalent to this but applicable to Transforms? Thanks for any help.
    Code (CSharp):
    1. public class RandomMoneySpawner : MonoBehaviour
    2. {
    3.     public GameObject objectToSpawn;
    4.     public Transform[] spawnPoints;
    5.  
    6.     public int amountToSpawn;
    7.     private int amountSpawned;
    8.  
    9.     public float spawnCollisionCheckRadius;
    10.  
    11.     void Start()
    12.     {
    13.         amountSpawned = 0;
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if(amountSpawned < amountToSpawn)
    20.         {
    21.             SpawnObject();
    22.         }
    23.     }
    24.  
    25.     public void SpawnObject()
    26.     {
    27.         Transform _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
    28.        
    29.             Instantiate(objectToSpawn, _sp.position, _sp.rotation);
    30.  
    31.         amountSpawned ++;
    32.     }
    33.  
    34.     public void ResetSpawnCount()
    35.     {
    36.         amountSpawned = 0;
    37.     }
    38.    
    39. }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,938
    Transform.position
    is a Vector3 and can be used as a positional coordinate for any API methods that need it, such as the various Physics methods that want a position, such as CheckSphere.
     
  3. Mr_Packer12

    Mr_Packer12

    Joined:
    Aug 11, 2022
    Posts:
    30
    Hmm I am not too sure. This is how I had it and it gave me the error: Cannot convert from UnityEngine.Transform to UnityEngine.Vector3. Line 33:
    Code (CSharp):
    1. {
    2.     public GameObject objectToSpawn;
    3.     public Transform[] spawnPoints;
    4.  
    5.     public int amountToSpawn;
    6.     private int amountSpawned;
    7.  
    8.     public float spawnCollisionCheckRadius;
    9.  
    10.     void Start()
    11.     {
    12.         amountSpawned = 0;
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if(amountSpawned < amountToSpawn)
    19.         {
    20.             SpawnObject();
    21.         }
    22.     }
    23.  
    24.     public void SpawnObject()
    25.     {
    26.         Transform _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
    27.  
    28.         if(!Physics.CheckSphere(_sp, spawnCollisionCheckRadius))
    29.         {
    30.             Instantiate(objectToSpawn, _sp.position, _sp.rotation);
    31.             amountSpawned ++;
    32.         }
    33.     }
    34.  
    35.     public void ResetSpawnCount()
    36.     {
    37.         amountSpawned = 0;
    38.     }
    39.    
    40. }
     
  4. Mr_Packer12

    Mr_Packer12

    Joined:
    Aug 11, 2022
    Posts:
    30
    Line 28* sorry.
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,938
    Yes... like I said you use
    .position
    property of Transform like you already are in your
    Instantiate
    call.
     
  6. Mr_Packer12

    Mr_Packer12

    Joined:
    Aug 11, 2022
    Posts:
    30
    Oh I see, yeah I got it thank you!