- Home /
How to spawn player randomly in one of certain positions?
Hey, I have for example 10 certain 2D positions in my scene and I need to spawn my player randomly in one of these positions, how do I do that??
I tried to find answer to my question from here, but because my first language isnt english i think i used wrong words on the search... thanx anyway
Answer by hiiii5 · Sep 20, 2014 at 04:02 PM
Since you are using javascript I will only attempt to translate as much as I can from C# as I am unfamiliar with its syntax in unity.
Here is the concept:
Place all points in the scene.
Pass in these points through variables; this means, using the inspector you can drag and drop the points onto the variable instances.
In the Start method, you want to create a random number and store it only temporarily; this means, create a variable in start and not in the "global" space.
This number will be allowed to go up to your number of positions -1. ie, if I have 10 positions, I only want the number to go up to 9 because zero can also be checked in this instance.
Now, check through if statements, what the number is then put something like this line,
Transform.transform.position = newPosition;
Hopefully this works well for you. I have allowed editing of this answer to allow for new and better information to come up and also if someone wants to add a full javascript file example, they can.
Your answer
Follow this Question
Related Questions
Random loot - rarity 3 Answers
How can i destroy cubes that is randomly generated when i have passed them? 1 Answer
Spawning different items from different prefabs 2 Answers
Advanced Wave Spawner (Spawn Waves in random order) 1 Answer
Create a function that spawn player (wich datas are stored in an array) randomly 4 Answers