- Home /
Player starts instantiate at random places?
Hi!!! I want my player start on different locations everytime I start a new game? How do I do that? Just now my player is in the game at the same startplace! Thanks for help!!!!
Answer by zannghast · Oct 19, 2010 at 02:35 AM
One simple solution is to:
1) create several empty GameObjects, tag or name them as "spawnpoint" or something.
2) cache the transform of these GOs at the start of your game (via list, array, hashtable, etc)
3) randomly search through your array of spawnpoint transforms and use the resulting transform.position as your player's transform.position.
All that within your Awake().
Simple Hint: You could create a SpawnPoint manager script for your player, attach that to a SpawnPointManager GameObject, and do all those simple steps there instead. That way, you'd be separating the spawning logic of your character from any other stuff you'd be doing.
Answer by Bravini · Oct 19, 2010 at 02:28 AM
create a script with the code below and attach it to an object on the scene then delete your Player object. drag your Playerpreab on the inspector to the var PlayerPrefab.
var xpos : int;
var ypos : int;
var zpos : int;
var PlayerPrefab : Transform;
function Start () {
//set this to the area you want the player to appear according to the size of your map xpos = Random.Range(1,100); ypos = Random.Range(1,100); zpos = Random.Range(1,100);
Instantiate(PlayerPrefab, transform.position(xpos,ypos,zpos), (transform.rotation(0,0,0));
}
sorry for the bad indentation, still new to this.
Answer by Bono · Oct 19, 2010 at 08:01 AM
I tried Bravinis idea but it does not work, somting is wrong with the code! It says at the second last line 14 - expcting ), found ;.
Thankyou for awnser! Hope you will get back! or sombody know whats wrong? Thankyou!!!!!
add a closing parenthesis to the last line like so:
Instantiate( PlayerPrefab, transform.position(xpos,ypos,zpos), (transform.rotation(0,0,0)) );
By the way, commenting on the 'answer' is the proper way to reply hehe. rules are rules xD