- Home /
spawn game object in random position
//hello i am building an AR app and trying to spawn game object in random position but i can't find the solution any help ? thank you ,hello , i am building an AR app and trying to spawn a game object in a random position but i can't find the solution any ideas ? // //code// using System.Collections; using System.Collections.Generic; using UnityEngine;
public class spawnscript : MonoBehaviour { public Transform[] spawnPoints; public GameObject [] balloons;
// Start is called before the first frame update
void Start()
{
StartCoroutine(Startspawning());
}
IEnumerator Startspawning(){
yield return new WaitForSeconds(4);
for (int i = 0 ; i < 3 ; i++){
Instantiate(balloons[i], spawnPoints[i].position, Quaternion.identity);
}
StartCoroutine(Startspawning());
}
} ,
Answer by privatecontractor · Jan 20 at 06:55 PM
Hi @jassemdegani,
So your spawn position is Vector3 if dealing with 3D or Vector2 if 2D. Each of them consist of float values. So for 3D:
[Serializefield] Vector3 m_maxPosition;
[Serializefield] Vector3 m_minPosition;
public void GetNewSpawPosition()
{
return new Vector3(Random.Range(m_minPosition.x, m_maxPosition.x), Random.Range(m_minPosition.y, m_maxPosition.y, Random.Range(m_minPosition.z, m_maxPosition.z));
}
So in inspector you will need to assign min and max values. Hope will work for you. Sry for mistakes in speling (editing on phone )
Your answer
Follow this Question
Related Questions
Moving GameObject to various position ? 1 Answer
Spawn game object in random position on screen 1 Answer
Instantiate a GameObject at the position of one of its child objects 1 Answer
Random Position For GameObject 1 Answer
How can I calculate and place a GameObject at a random place just outside the camera frustrum? 0 Answers