Unity2D: How to make script randomly pick a point within an object (X-axis)
Hi, is there a way of randomly spawning my objects on the y-axis. You see, I have a spawning script that randomly pick a point within an object (in this case a 3D cube) to spawn in a prefab, however my spawner only works ( randomly pick a point within an object to spawn) on the x-axis and doesn't spawn in randomly within the y-axis. This short video is a representation of what I am explaining. Thank you This is my spawning script:
public GameObject enemy;
public float spawnTime = 2;
void Start() {
InvokeRepeating ("addEnemy", spawnTime, spawnTime);
}
void addEnemy() {
var x1 = transform.position.x - GetComponent<Renderer> ().bounds.size.x / 2;
var x2 = transform.position.x + GetComponent<Renderer> ().bounds.size.x / 2;
// Randomly pick a point within the spawn object
var spawnPoint = new Vector2 (Random.Range (x1, x2), transform.position.y);
Instantiate (enemy, spawnPoint, Quaternion.identity);
}
}
Answer by TBruce · Aug 20, 2016 at 06:21 PM
It looks like you are using the same code to spawn your enemies from the different cubes (one on top one on right).
If that is not the case then your problem is that your code is reversed. What I mean is it should look like this
void addEnemy()
{
float y1 = transform.position.y - GetComponent<Renderer> ().bounds.size.y / 2;
float y2 = transform.position.y + GetComponent<Renderer> ().bounds.size.y / 2;
// Randomly pick a point within the spawn object
Vector2 spawnPoint = new Vector2 (transform.position.x, Random.Range (y1, y2));
Instantiate (enemy, spawnPoint, Quaternion.identity);
}
If you are using the same function to add enemies from the top and the right then your code need to look more like this
public GameObject enemy;
public float spawnTime = 2;
public bool fromTop = false;
void Start()
{
InvokeRepeating ("addEnemy", spawnTime, spawnTime);
}
// this will handle instantiating objects from the top or right
void addEnemy()
{
Vector2 spawnPoint = new Vector2 (transform.position.x, transform.position.y);
if (fromTop)
{
float x1 = transform.position.x - GetComponent<Renderer> ().bounds.size.x / 2;
float x2 = transform.position.x + GetComponent<Renderer> ().bounds.size.x / 2;
// Randomly pick a x point within the spawn object
spawnPoint.x = Random.Range (x1, x2);
}
else
{
float y1 = transform.position.y - GetComponent<Renderer> ().bounds.size.y / 2;
float y2 = transform.position.y + GetComponent<Renderer> ().bounds.size.y / 2;
// Randomly pick a y point within the spawn object
spawnPoint.y = Random.Range (y1, y2);
}
Instantiate (enemy, spawnPoint, Quaternion.identity);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612091218im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flag capture with spawning and destroying gameObject 0 Answers
[C#] Only one instantiate object is working, the rest is not.. Help meee 0 Answers
Unet: Spawn bullet - client does not have a reference of bullet 1 Answer
UNET 5.4 - Spawn an object from client...and move it around from client....? 1 Answer
Spawning limited GameObjects at a specific position not working 1 Answer