- Home /
Scene changing and multiple spawn points
So I have a bit of a problem. My game, is a 3rd person adventure game, and I have multiple scenes for the game, however there is an issue I can seems to find an answer for. My game is going to have an overworld that links you to different area's much like it does in zelda. However, I can't seem to figure out how to have multiple spawn points on one map, you touch one and it switches you to the next scene which would put you at the entrance you should spawn at appropriately and if you were to hit the spawn point at that entrance you would spawn back and the entrance in the overworld. I have seen scripts that deal with single spawn points for a starting spawn point when the player dies, etc, but none for multiple spawn points. I have my characters code setup so he doesn't unload and gets transferred over to the next scene. So is there anyway to have this happen. I am mainly skilled in c++ so I was messing with unity and have a fair amount of stuff figured out, but this is just stumping me, however is there any code or anyone who can help me with the coding on this.
Answer by Kawaburd · Jul 29, 2013 at 03:53 AM
My first instinct would to bundle whatever handles your LoadLevel function with an if or switch statement to check where the player's coming from. Then when it's called, change the players's position according to what it used to be, or what Application.loadedLevel returns, or anything else that would affect where you'd want the spawn point.
Interesting, and I kinda thought that might be needed, and one of the issues unity has is me making a public int or float cause the complier says to me that it wants to make it a bool value. The way I would think it would work is like, have only so many spawn points per map, say 9, and on trigger with one, when you collide, it loads the map it's connected to (which I wish I could figure out how to just use one script that allows me to type in the name for the map in the inspector, that way the script is reusable), and it loads based on an if statement. For example, if transit = 1, then loadscene (which would then have what ever the code is for me to type in the name of the scene) and put player spawn at spawn point (specify the number as well so if you came in from spawn point 1 you go to the next area's spawn point 1). I can't seem to figure the script out for that, though the most I would have for spawn points on one map would be like 9 or ten so, do you think you could write a script that would do the description given for the spawn points. It seems like it would be an effcient way to handle the spawn points since most of it involves the collsion and a variable on the player (probably transit = 2 then you go to spawn point two and vice versa). It would mean so much if you could do that.
This is only a start (and be warned, I'm still really green with Unity so it could have any number of bugs), but it should at least give a general idea.
function levelSwitch (transit: int, playerPos: Vector3) {
switch (transit) {
case 1:
Application.LoadLevel("Overworld"); // Assu$$anonymous$$g this one would lead to a default point?
case 2:
Application.LoadLevel("Overworld");
playerPos = (107.9f, 1.2f, 22.6f); // And these values, for instance, just looked up for point #2 in the inspector.
case 3:
Application.LoadLevel("Some Odd Dungeon Or Wherever"); // And this one had a default too?
}
return playerPos // Pass it off however you need and have another script do the actual location change with it.
}
I actually tried something and it's not working, as the collision won't trigger anything even with the box ticked is trigger. here is the code I just came up with.
var myLevel : String;
var transit : int;
function OnCollisionEnter (myCollision : Collision)
{
if (transit == 1){
Application.LoadLevel(myLevel)
}
else
if (transit == 2){
Application.LoadLevel(myLevel)
}
} //transition code
var transitObj : String;
var spawnPoint : int;
function OnCollisionEnter (myCollsion : Collision)
{
if (myCollision.gameObject.name == "transit1")
{
spawnPoint = 1;
}
else
if (myCollision.gameObject.name == "transit2")
{
spawnPoint = 2;
} //player code
It seems like it should work, and I can add the player spawning next to the transition object after I work out everything. But does this seem like it should work and if so, why is the collsion not working?
I've tried the rigged body collisions, box colliders, etc and nothing seems to work. $$anonymous$$y character keeps moving through the object as if there is nothing being triggered and I am unsure of what the problem is.
Your answer
Follow this Question
Related Questions
Using player as prefab ? 1 Answer
Player Spawn problems 2 Answers
How to make a game with never ending waves of zombies like in COD zombies? 0 Answers
2D Player Spawn Issue 0 Answers