Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by mdsmith98 · Feb 17, 2014 at 11:37 PM · spawnspawningloadlevelapplication.loadlevel

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mdsmith98 · Feb 18, 2014 at 01:35 AM 0
Share

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.

avatar image thaiscorpion · Feb 18, 2014 at 06:20 PM 0
Share

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.

avatar image mdsmith98 · Feb 18, 2014 at 10:04 PM 0
Share

Its making a little more sense... I have two more questions...

  1. How exactly do you make a global script or what you are talking about that the script can be accessed by all scenes?

  2. 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.

avatar image thaiscorpion · Feb 18, 2014 at 10:21 PM 0
Share

Don't worry it takes a lot of time to understand so many things :D

  1. 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.

  2. 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.

avatar image mdsmith98 · Feb 19, 2014 at 02:32 AM 0
Share

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?

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges