- Home /
spawn points/ player transition from level to level with multiple exits and entrances
The following script is supposed to a) look at the last level loaded. b) spawn the player to a game object position in the new scene/level loaded.
I am failing to integrate it properly. I am new to scripting. I have tried to integrate the Awake() function but failed. I suspect this is the trouble? Code/syntax help appreciated.
I have set the var character1 and fromThisLevel on the game object/spawn point with script attached.
The new level is loading with a grey screen and no player instantiated.
var character1: GameObject; var fromThisLevel = 0;
function OnLevelWasLoaded (level : int) { if (level == fromThisLevel) { print ("Woohoo"); Instantiate(character1,transform.position,transform.rotation);}}
Answer by Bunny83 · Feb 01, 2011 at 05:36 PM
If you want a continuous level like in Halflife where when you enter the end-of-level trigger it loads a new level around you. The two levels are overlapping a bit so that after the loading it looks like you're still in the same level at the same position. There are even levels that have multiple transition points like you mentioned. In Halflife they use "info_landmark" nodes inside the levels to match up the two levels.
The player object should not be deleted in this case and don't need to be instantiated again. Just be sure to mark the player object with DontDestroyOnLoad.
That's how a landmark node script could look like:
public var name : string;
private static var m_oldPosition : Vector3;
private static var m_oldName : string;
public static function GetOffset() : Vector3 {
var landmarks : Landmark[] = FindObjectsOfType(Landmark);
for (int i = 0; i < landmarks.Length; i++) {
if (landmarks[i].name == m_oldName) {
return (landmarks[i].transform.position - m_oldPosition);
}
}
return Vector3.zero;
}
public function UseLandmark() {
m_oldName = name;
m_oldPosition = transform.position;
}
On the end-of-level trigger:
var levelname : string;
var landmark : Landmark;
function OnTriggerEnter() {
landmark.UseLandmark();
Application.LoadLevel(levelname);
}
and inside your gamelogic :
var playerObject : GameObject;
function OnLevelWasLoaded (level : int) {
Vector3 offset = Landmark.GetOffest();
playerObject.transform.position += offset;
}
Answer by DannyJax.du · Oct 12, 2010 at 03:10 AM
look in the Lerpz tutorial, it has a script in it that if Lerpz falls off the platform and hits a collision detection, he disappears and re-spawns on a platform he previously stored his location to. That script can be converted to do what you want. There are as many spawn locations in the level as you place. I placed like 3, and they worked, I then placed about 10 more and they all worked.
I think he meant between different levels, not in one scene.
Answer by superventure · Feb 21, 2011 at 06:37 AM
I was grueling over the exact same problem, and JUST thought of this- It's working out for me, I hope it helps you too-
The problem is that we want to begin at one of any specific points. We were looking at it in a dead end way though. When you load a level, you usually have one spawn point with code to 'JUST START HERE'. We need be able to carry on info from the last level to tell the new level that you want to start at ANY specific point you specify.
I think Bunny83 just descirbed this but I don't understand all that :p...
I thought this could be done with a kind of scoring method that all spawn points could look at. This could done in a much simpler way, so fix it up how you want
Make 3 (or however many) spawn points you want in your levels and tag them startpoint1, startpoint2, etc, and add something like this to them ( I used empty game objects)
function Start() {
if(Player.teleporter == 1){
start1();
}if(Player.teleporter == 2){
start2();
}if(Player.teleporter == 3){
start3();
} }
function start1(){
var Player = GameObject.Find("Player"); var startpoint1 = GameObject.FindWithTag("startpoint1").transform;
Player.transform.position = startpoint1.position;
}
function start2(){
var Player = GameObject.Find("Player"); var startpoint2 = GameObject.FindWithTag("startpoint2").transform;
Player.transform.position = startpoint2.position;
}
function start3(){
var Player = GameObject.Find("Player"); var startpoint3 = GameObject.FindWithTag("startpoint3").transform;
Player.transform.position = startpoint3.position;
}
Have the actual 'teleporting game object' (a door, a floor pad, something) and add a collider set as a trigger and add this
function OnTriggerEnter(other : Collider) {
Player.teleporter = 2; // whatever number to correspond to where you want to spawn
Application.LoadLevel("cave");
}
on the player's script add this in the update function
DontDestroyOnLoad (this);
and add at the top of the script
static var teleporter = 0;
Do you get the set up? When you hit the exit or 'teleporting game object', you set the players teleporter number to the specifically numbered spawn point you set in the new level.
You'll have to make multiple application.Loadlevel scripts to set the specific numbers, but it works in the long run.
I must be missing something here I'm trying it out and I get a "$$anonymous$$ identifier: 'Player'." error message??
Your answer
Follow this Question
Related Questions
Score Question Java Script, problem 1 Answer
Player spawn after touching 0 Answers
Spawn Player when Room Starts - (Multiplayer) 1 Answer