Hello,
I’ve got question about spawn area. I would like to spawn objects only in certain zone (marked on the picture), but always above player. I’ve got only spawn in camera boundary, but because of that some of the object spawn out of level.
Is there any simple way to do that?
Thanks in advance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class deploySquare : MonoBehaviour
{
public GameObject squarePrefab;
public float respawnTime = 0.01f;
private Vector2 screenBounds;
// Start is called before the first frame update
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
StartCoroutine(squareWave());
}
private void spawnSquare ()
{
GameObject a = Instantiate(squarePrefab) as GameObject;
a.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x), Random.Range(-screenBounds.y, screenBounds.y));
}
IEnumerator squareWave()
{
while(true)
{
yield return new WaitForSeconds(respawnTime);
spawnSquare();
}
}
}
The best way to spawn things in a pre-made level is to put GameObjects (invisible with nothing on them) where you want the spawn to happen, then drag that gameObject into your script to use as a position.
You can also add gizmos to these invisible gameObjects so they show up in scene when you’re working and editing your level, or even write your own custom gizmos with OnDrawGizmos() or OnDrawGizmosSelected().