- Home /
Create Objects at Random Place on a Plane
Hello guys, I've been working on "Random" things. So I couldn't get it exactly.
I've got 5 diffrent NPC spaceships. 2 types of asteroid, 3 types of bonus things and a Spawner Plane.
So, Spawner Plane's scale = (0.6, 0.6, 0.6).
I want to create thoose objects at inside of the plane. But i dont know how to assign them into array. Could anyone help me thanks.
Age 14, From Turkey.
Answer by Aladine · Jan 27, 2014 at 07:22 PM
Hi,
in order to create an array of game objects (better go with Transforms) you need something like this :
1st create a list
public List<Transform> myList = new List<Transform>();
2nd add your objects to that list, for now i'll advice you to do it in the inspector by changing the size of your list to 3 and then just drag and drop your game objects into the list.
now is the random placing part and actually it's quite easy, all you need is to generate a random Vector3 (or Vector2 if it's 2d) based on your plane position and scale
Example
//this generate random coordinates isinde the plan, assuming that you already get your plan and stored it in a variable called plane float randomX = Random.Range (plane.transform.position.x - plane.transform.localScale.x / 2, plane.transform.position.x + plane.transform.localScale.x / 2); float randomY = Random.Range (plane.transform.position.y - plane.transform.localScale.y / 2, plane.transform.position.y + plane.transform.localScale.y / 2); float randomZ = Random.Range (plane.transform.position.y - plane.transform.localScale.z / 2, plane.transform.position.y + plane.transform.localScale.z / 2); Vector3 randomPosition = new Vector3 (randomX, randomY, randomZ);
now back to your list, you need to assign that random position to your objects (keep in mind to redo the process for each objects otherwise they will all go to the same position)
myList[0].position = randomPosition;
do the same thing for other elements but only after re-randoming the position Important in order to use Lists make sure to import Generic so you have to add this line in the top
using System.Collections.Generic;
Can i use Array $$anonymous$$ethod is$$anonymous$$d of List ?
Yes you can, and please tick as true if the answer answers you correctly :)
Answer by Exalia · Jan 28, 2014 at 12:43 PM
If you want to put your GameObjects into an array, there are several ways depending on how your scene is set up and your preferences.
If your objects already exist in your scene you can make your array public and drag them into the array using the inspector. Just declare your array in a script and drag them in.
public GameObject[] gameobjects;
If you are using prefabs you can use the same technique.
If you don't want to do it this way you can Tag your objects. Then iterate through the objects in your scene until you have the ones you want. This is okay for initialisation but will increase CPU thread time dramatically at runtime.
GameObject[] gameobjects;
void Start()
{
gameObjects = GameObject.FindObjectsWithTag("ArrayObjects");
}
I hope this helps
P.S : Planes are once sided with Y value that defines which way it is facing
are you defining a plane or a volume?
(0.6,0.6,0.6) is the same as (0.6,1,0.6) in a plane, (0.6,-1,0.6) would mean it is facing the other way.
Volume, i know it doesn't matter until plane's Y axis = 0.
Your answer
Follow this Question
Related Questions
Instantiate random object from Array 2 Answers
Spawn objects at random position ON a 2D object. 1 Answer
Invoke Ball Obstacle based on Score 0 Answers
random spawning of platforms. Overlapping and 2x2 spawning issue. 1 Answer
I want to swap randomly 5 blocks (block has 5 diffrent color) and spawn in vertical line 1 Answer