- Home /
Help on specific spawn position for a single prefab
Hi I am having real trouble trying to figure this out at the moment I have a script which allows me to be able to insert as many enemy prefabs in to the inspector and will allow me to spawn them within a local boundary, my problem is I have to brefabs one which is a butterfly and a bird, the butterfly is fine and that needs to be able to spawn anywhere in the local boundary but I need a way to be able to spawn the bird randomly but no lower than 5 in the y-axis, can anyone help, be much appreciated as im racking my brian round it, I know its probably some think simple that im over looking but any help would be great, THANKS.
using UnityEngine;
using System.Collections;
public class EnemySpawnerController : MonoBehaviour {
private Collider collider;
int hazardCount;
public int hazardCountMax;
public int hazardCountMin;
int WaveWait;
public int WaveWaitMin;
public int WaveWaitMax;
float WaitMax =10f;
float WaitMin = 5f;
public float spawnWait;
public float SpawnValueX;
public GameObject[] EnemyPrefabs;
void Start ()
{
collider = GetComponent<Collider> () as Collider;
StartCoroutine (SpawnWaves ());
}
// Update is called once per frame
void Update ()
{
}
private Vector3 MakeRandomSpawnPosition()
{
Vector3 local = collider.transform.position;
local.x = SpawnValueX - (collider.transform.localScale.x/2);
local.y = Random.Range(local.y, local.y + collider.transform.localScale.y) - (collider.transform.localScale.y /2);
local.z = 0f;//Random.Range(local.z, local.z + collider.transform.localScale.z) - (collider.transform.localScale.z /2);
return local;
}
IEnumerator SpawnWaves ()
{
float currentWait = Random.Range (WaitMin, WaitMax);
WaveWait = Random.Range (WaveWaitMin, WaveWaitMax);
hazardCount = Random.Range (hazardCountMin, hazardCountMax);
yield return new WaitForSeconds (currentWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
GameObject gameObjectInstance = MakeRandomGameObject(MakeRandomSpawnPosition());
//Instantiate (enemyPrefab, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (WaveWait);
}
}
private GameObject MakeRandomGameObject(Vector3 position)
{
GameObject enemyPrefab = EnemyPrefabs[Random.Range(0, EnemyPrefabs.Length)];
Vector3 spawnPosition = MakeRandomSpawnPosition();
Quaternion spawnRotation = Quaternion.identity;
GameObject gameObjectInstance = Instantiate(enemyPrefab, spawnPosition, Quaternion.identity) as GameObject;
return gameObjectInstance;
}
void OnTriggerExit (Collider other)
{
if (other.gameObject.tag == "Enemy")
{
Destroy (other.gameObject);
}
else if(other.gameObject.tag == "PickUp")
{
Destroy (other.gameObject);
}
}
}
you already have $$anonymous$$akeRandomSpawnPosition method, you need to change it as y position will be greater than 5.
if local.y < 5, random range would be starting from 5?
or you could just make an empty game object and make the player spawn from there
Answer by sedativechunk · Apr 03, 2014 at 03:08 AM
What you are looking for is a randomizer. Randomizers can help bring your game to life and help create unpredictablility which replicates that of A.I. decisions.
// Create a random position for the game object
// For now I'll just use the transform.position but you could create a new vector 3
Vector3 position = transform.position;
// Generate a random range for the position of your object on the X and Y axis (you could also use the Z axis), this could will generate a random number between 1 and 5. If you want different ranges/bounds you can modify them and even include negative numbers
float x = Random.Range(0, 5f);
float y = Random.Range(0, 5f);
// Add the random values to the position
position.x = x;
position.y = y;
// Update the transform position
// It's best to use a new variable for C# because you cannot directly modify the transform position
transform.position = position;
Just be careful with randomizers. They can get slightly resource demanding. I would only use it in the "Start()" call when instantiating an object. But doing that should allow your objects to spawn randomly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141749im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Link GUIText to a prefab? 2 Answers
Multiple Doors and Buttons 0 Answers
Accessing a certain prefab and it's children 1 Answer
Why have some of my prefabs and animations files gone blank?? 1 Answer