x


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!!!!

more ▼

asked Oct 18 '10 at 11:37 PM

Bono gravatar image

Bono
1 2 2 2

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Oct 19 '10 at 02:35 AM

zannghast gravatar image

zannghast
526 11 14 26

(comments are locked)
10|3000 characters needed characters left

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!!!!!

more ▼

answered Oct 19 '10 at 08:01 AM

Bono gravatar image

Bono
1 2 2 2

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

Oct 20 '10 at 09:03 AM zannghast
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Oct 19 '10 at 02:28 AM

Bravini gravatar image

Bravini
1.1k 6 11 29

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1667
x801
x570
x156
x5

asked: Oct 18 '10 at 11:37 PM

Seen: 1200 times

Last Updated: Oct 18 '10 at 11:37 PM