- Home /
How to choose coordinates on Application.loadlevel
I am currently working on a game where the user can enter a building by walking up to the building and pressing the E key. When the player walks up to the building, it says "Press E to enter (building name)", in this case the warehouse. That part works fine, the player easily loads into the warehouse when they stay on the trigger and press the E key.
When the player is in the warehouse, they can walk up to the warehouse door (Trigger) and press E to exit, and it leads them to my island scene. Now to my question. I am wondering when the player presses E to exit the warehouse, how can I determine where they spawn? I want the player to load outside of the warehouse, the same place they pressed the E key to enter the warehouse, so just outside the entrance to the warehouse. This may be a simple question, but I have no idea how to go about doing this.
I've looked at other topics similar to this, but couldn't figure out how to tie it in with my question.
Bear with me, I have only been in Unity for about a week now, and have started writing scrips a few days ago, so I am very new to scripting, and this is my first post on UnityAnswers.
LoadWarehouseInterior.js (This is on the island scene, but this brings them inside the warehouse):
#pragma strict
var hud : boolean = false;
var checkKeyDown : boolean = false;
function OnGUI(){
if(hud == true){
GUI.Box(Rect(325,320,200,40), "Press E to enter Warehouse.");
if(Input.GetKeyDown("e")){
Application.LoadLevel("WarehouseInterior");
}
}
}
function OnTriggerStay () {
hud = true;
}
function OnTriggerExit (){
hud = false;
}
LoadWarehouseExterior.js (This should take them to my island, in the specified location):
#pragma strict
var hud : boolean = false;
var checkKeyDown : boolean = false;
function OnGUI(){
if(hud == true){
GUI.Box(Rect(325,320,200,40), "Press E to exit the Warehouse.");
if(Input.GetKeyDown("e")){
Application.LoadLevel("");
}
}
}
function OnTriggerStay () {
hud = true;
}
function OnTriggerExit (){
hud = false;
}
If it helps, I would like the player to spawn at the following coordinates - X: 2731, Y: 121, Z: 2071
Since I will have numerous buildings the player can enter, I would appreciate it if the player spawns to the certain specified spot, since each exited building should have its own spawn point.
So how would I go about doing this? Like I said, I am new to unity and scripting, so please be easy with me in your answers, and help is appreciated!
Answer by thaiscorpion · Feb 18, 2014 at 12:24 AM
Well for things like this it is a great idea to have a class that can control and manage the whole game, in this case the room transactions. You could create a class script that can be accessed globally, and when you load every level there should be a script in that game that checks what door the player came from and move the player acording.
Something like this:
static class GameController {
var doorID : int;
function EnterDoor(thisDoor:int){
//Set you doorID here and load your new scene
doorID = thisDoor;
//To do this you would just write this in any script
// GameController.EnterDoor(1); when the player presses E on door 1
}
}
Then when you have loaded a scene check what the door id was and move the player:
function Start(){
if (GameController.doorID == 1){
//Move position according
}
}
Hope that helps, if you need any more help understanding just let me know.
Sound useful... but how would I go about assigning an ID to a trigger? Sorry if this is a stupid question... or if it automatically does it... how would I go about doing that? Because what I have is a cube that is transparent, and it is set as a trigger. Also... could I apply this script to my current code above where I display a "Press E to enter (building)", then when E is down it loads level? Where would I add in the Application.LoadLevel? How would I make the player go back to the "door" or the collider on the main island? Sorry to be a bother... I am very new to unity as I stated and I don't know a whole lot about this.
An easy way would be adding a script with the trigger event in it that you add to each trigger, and that script should have a variable ID in it that you can change from the inspector. This way u just add the script to any door or trigger u want and change the ID. When the player triggers it the script can execute the enterDoor function from the GameController Class.
If you see in the class code on line nº9 you can add your Application.LoadLevel there.
I hope thats easier to understand, if your new to unity it can be very confusing at first. Let me know if anything wasn't clear.
Its making a little more sense... I have two more questions...
How exactly do you make a global script or what you are talking about that the script can be accessed by all scenes?
How exactly would you deter$$anonymous$$e whether or not the player has entered a "door?" Would I just make the class script on the main island, then scripts in each loaded scene that will deter$$anonymous$$e the ID of the "door" (or trigger)? If so, lets say the entered trigger had the ID of 1 through the script, how exactly would I post that (or transfer) it over from the main island script to the loaded scene's script, then to the main island again when they exit the door? Is there a certain line of code that I am needing?
Sorry for such nooby questions... I am very new to this.
Don't worry it takes a lot of time to understand so many things :D
If you create a script with the same name as the class that I wrote. like: GameController.js with the 'class GameController' in it. If you make the class static you will be able to access it from anywhere. Just by writing 'GameController'. For instance if you wanted a variable called 'speed', you just use GameController.speed anywhere u want.
If you make a script with a variable ID that can be changed in the inspector that executes a function in your GameController and sends it the ID of the door. That script could send it when the player presses E and is near it. Then when you load a scene you can check the variable lastRoom from the GameController to check what room you came from and then position the player where u want.
I'm about to play around with this and see if I can get it working... however if you take a look at my code it says
function OnGUI(){
if(hud == true){
GUI.Box(Rect(325,320,200,40), "Press E to enter Warehouse.");
if(Input.Get$$anonymous$$eyDown("e")){
Application.LoadLevel("WarehouseInterior");
}
}
}
How would I apply the
function EnterDoor(thisDoor:int){
//Set you doorID here and load your new scene
doorID = thisDoor;
//To do this you would just write this in any script
// GameController.EnterDoor(1); when the player presses E on door 1
}
to my function OnGUI()? Is it possible to nest a function within another function?
And also in order to use the GameController.js I would need to add this component to all objects I want to use with it, correct?
Answer by mdsmith98 · Feb 21, 2014 at 03:57 AM
Finally I got it working. For anyone else that is wondering a similar thing, here is my script I used:
LoadWarehouseInterior.js
#pragma strict
var hud : boolean = false;
var checkKeyDown : boolean = false;
static var doorID : int;
function OnGUI(){
if(hud == true){
GUI.Box(Rect(325,320,200,40), "Press E to enter Warehouse.");
if(Input.GetKeyDown("e")){
doorID = 1;
Application.LoadLevel("WarehouseInterior");
}
}
}
function OnTriggerStay () {
hud = true;
}
function OnTriggerExit (){
hud = false;
}
When the user enters my cube collider on the main island, they can press the E key to enter the door, then it sets the id of the door to one, then loads the warehouse.
LoadWarehouseExterior.js
#pragma strict
var hud : boolean = false;
var checkKeyDown : boolean = false;
static var doorIDLoad : int;
function OnGUI(){
if(hud == true){
GUI.Box(Rect(325,320,200,40), "Press E to exit Warehouse.");
if(Input.GetKeyDown("e")){
Application.LoadLevel("Island");
}
}
}
function OnTriggerStay () {
hud = true;
}
function OnTriggerExit (){
hud = false;
}
Basically, all the above script does is to check if the player is on the collider, if so and they press the E key, they will exit to the Island scene.
FindDoorLocation.js
#pragma strict
function Awake() {
if(LoadWarehouseInterior.doorID == 1){
var player = GameObject.Find("First Person Controller");
player.transform.position = Vector3(2731,125,2071);
}
}
All the script above does is to verify that the doorID is equal to 1, and if it is (which it is) then it will move the player to the coordinates I specified, before showing the player the loaded scene. If you choose to use this script, of course you will have to change the doorID if you have multiple doors in your scene.
Your answer
Follow this Question
Related Questions
spawning game objects 1 Answer
[Help] How Do I Randomly Spawn Game Objects On Specific Coordinates? 3 Answers
Load level and transform 1 Answer
how to destroy a object only in game and not the prefab 2 Answers
Spawning bug with scale and position. 0 Answers