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??
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.