Enter/ Exit Area

I’m working on a project and it’s going well however I’m having a small problem I was hoping I could get help with.

In each level I have two spawn points for the player. The entrance and exit. When the player leaves an area I would like it to return to the appropriate spawn point.

For instance if leaving section B of stage 1 to return to section A of stage one, it would start the player in the spawnpoint exit for section A.hope that wasnt’ confusing

I have empty game object spawn points set up, I’m just having trouble writing the code. On player awaken, a load char function is run and there is no instantiation because the player character is already a part of the scene. Any other questions I’d be happy to answer.

In each level, keep an object with a script on it that keeps track of spawnpoints, with an array of transforms or something. Give it a unique tag, such as "SpawnInfo", and then have a second script which keeps track of which point to spawn at for each level, with an array of integers. The second script should use DontDestroyOnLoad, to stop it from getting deleted when you change scenes.

In your 'GameEngine' script (or whatever you use to keep track of persistent ingame information)

public int[] levelSpawns;

void Start ()
{
    levelSpawns = new int[Application.levelCount];
    gameObject.tag = "Engine";
    DontDestroyOnLoad(gameObject);
}

Then, when you leave a level, have a few lines that go like this-

public void LeaveLevel(int nextLevel, int targetSpawnPoint)
{
    GameEngine engine = GameObject.FindWithTag("Engine").GetComponent<GameEngine>();
    engine.levelSpawns[nextLevel] = targetSpawnPoint;
    Application.LoadLevel(nextLevel);
}

Then on your player (when you load the level), set the position correctly, and you're done!

void OnLevelWasLoaded(int newLevel)
{
    GameEngine engine = GameObject.FindWithTag("Engine").GetComponent<GameEngine>();
    int spawnNumber = engine.levelSpawns[newLevel];
    SpawnManager manager = GameObject.FindWithTag("SpawnInfo").GetComponent<SpawnManager>();
    Transform spawnPoint = manager.spawnPoints[spawnNumber];

    transform.position = spawnPoint.position;
    transform.rotation = spawnPoint.rotation;
}

(Posted in C# because you didn't specify, and I prefer the language. If it's that important to you, I can translate it to JS if you need it)

EDIT Heres some JS for people who want that.

ENGINE-

var levelSpawns : int[];

function Start ()
{
    levelSpawns = new int[Application.levelCount];
    gameObject.tag = "Engine";
    DontDestroyOnLoad(gameObject);
}

Bit for exiting level-

function LeaveLevel(nextLevel : int, targetSpawnPoint : int)
{
    var engine : GameEngine = GameObject.FindWithTag("Engine").GetComponent(GameEngine);
    engine.levelSpawns[nextLevel] = targetSpawnPoint;
    Application.LoadLevel(nextLevel);
}

Bit on level load

function OnLevelWasLoaded(newLevel : int)
{
    var engine : GameEngine = GameObject.FindWithTag("Engine").GetComponent(GameEngine);
    var spawnNumber : int = engine.levelSpawns[newLevel];
    var manager : SpawnManager= GameObject.FindWithTag("SpawnInfo").GetComponent(SpawnManager);
    var spawnPoint : Transform = manager.spawnPoints[spawnNumber];

    transform.position = spawnPoint.position;
    transform.rotation = spawnPoint.rotation;
}