- Home /
Insantiating randomly within a collider.
Hi Unity community!
What I'm trying to find out is this; how do I insantiate trees within a collider/boundary. Just one or two, every 10 seconds or so. Here is a picture.
The island itself is not a terrain but rather a plane. I have, however, set up an invisible terrain on top, the exact same size (although I cannot think of a reason to use this).
TLDR; how do I insantiate a prefab every 10 seconds within a collider GameObject or some boundary of some sort.
Thanks!
Answer by DoTA_KAMIKADzE · Apr 01, 2015 at 05:03 PM
Here is rough example, it should be enough for understanding, but be sure that you have only 1 instance of this script in whole gameworld:
[SerializeField]
private GameObject treeObj;
private const float halfWidth = 300f;
private const float halfHeight = 300f;
private const int maxTreePerVawe = 2f;
[SerializeField]
private float waveWait = 10f;
private void Start()
{
StartCoroutine(SpawnWaves());
}
private void SpawnTree(int count)
{
for (int i=0; i < count; i++)
{
// considering that Y:0 is the flat position of your plane
Vector3 spawnPosition = new Vector3(UnityEngine.Random.Range(-halfWidth, halfWidth), 0f, UnityEngine.Random.Range(-halfHeight, halfHeight)); // you can get those values from "plane's "width/height"" if you want instead of adding them in script
spawnPosition += transform.position; // where transform.position is basically the dot in the world which represents the center of your "plane", can be removed if it is 0:0:0, or changed if script is not attached to "plane"
GameObject newTree = Instantiate(treeObj, spawnPosition, Quaternion.identity) as GameObject;
// do here with newTree anything else that you want to do on initiation, like customizing Randomly size for example
}
}
IEnumerator SpawnWaves()
{
yield return new WaitForSeconds(waveWait);//initial wait
while (playerModel.isAlive)
{
SpawnTree(UnityEngine.Random.Range(1, maxTreePerVawe));
yield return new WaitForSeconds(waveWait);//continue loop wait while player is alive or some other condition
}
}
Thanks for the script, but it's not really what I am looking for. Although if I did some heavy tweaks to it, I could get it to work with my project. I was looking more for a sort of insantiate-within-a- collider kind of script if Unity allows that kind of thing. I really appreciate it though!
As I have commented in script you can set those values to whichever you need, e.g. fetch values from your collider like this:
BoxCollider coll = GetComponent<BoxCollider>();
Vector3 collSize = coll.size;
Vector3 collCenter = coll.center;
And adjust randomization of spawn position accordingly, to fit your collider sizes.
P.S. Just remove halfWidth, halfHeight replacing those values with $$anonymous$$/max allowed values that you'll get from your collider - that simple.
Your answer
Follow this Question
Related Questions
Vault Over Collider Based Mesh 3 Answers
Using a collider to define the boundary of a level? 3 Answers
My object is going through boundaries Problem! 1 Answer
How to set a boundary around an object? 1 Answer
Internal collisions 1 Answer