x


How do I create an effective spawn point script for multiple scenes and change music each scene?

Hi, I'm trying to create spawn points and an automatic music changer, but I can't seem to figure out how to make either one work.

The reason I need spawn points is I've got these portals that teleport the player to each new level, and the portals play a sound effect upon collision with the player. But in order to play the sound effect all the way through, the audio source has to be attached to an object that doesn't get destroyed when the new scene loads. Now I've managed to get that part working fine by setting both the camera and player character to DoNotDestroy (the teleportaion sound effect is attached to an empty game object called "Portal Sound", which is itself attached to the main camera), but now the player character retains his old coordinates from wherever he was standing in the previous scene when the new scene loads, and I need to be able to specify an exact location for him to spawn at in the new scene.

Also I need the music to change automatically when each new scene loads, as each level has its own theme music.

EDIT: Here's the code I've got so far, but it doesn't seem to work:

var SpawnPoint : GameObject;

function OnLevelWasLoaded()
{
    SpawnPoint = GameObject.FindWithTag("SpawnPoint");
    transform.position(SpawnPoint.transform);
} 
more ▼

asked Apr 28 '10 at 07:02 PM

TaintedSeraphim gravatar image

TaintedSeraphim
1 1 1 3

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

1 answer: sort voted first

Just attach an empty GameObject to each scene called SpawnPoint and on scene load set the players transform.position to the position of SpawnPoint. To change music you could just create a second audio source and crossfade with the old one.

You could do all this in the OnLevelWasLoaded() method.

more ▼

answered Apr 28 '10 at 07:17 PM

StephanK gravatar image

StephanK
6k 40 53 93

And how exactly do I set the player's transform.position to the position of SpawnPoint? I've edited the original question to include my code.

Apr 28 '10 at 07:49 PM TaintedSeraphim

instead of transform.position(SpawnPoint.transform) do this: transform.position = spawnPoint.transform.position;

On another note variables should always start with a small letter to make it easier to differentiate them from functions and types.

Apr 28 '10 at 09:50 PM StephanK
(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:

x453
x389
x160
x50
x37

asked: Apr 28 '10 at 07:02 PM

Seen: 4083 times

Last Updated: Apr 28 '10 at 09:36 PM