Teleporting player on key press
I have added a door with a trigger next to it, I would like it so that when the player hits the e key they teleports to a new scene. I have use this yet it doesn't work.
if (Input.GetKey("e")) Application.LoadLevel("Village");
Answer by EmHuynh · Feb 15, 2016 at 09:48 AM
Hello, @GreenWizard33! In your script, call SceneManagement
to load the scene if the player pressed the teleport key.
using UnityEngine.SceneManagement;
if( Input.GetKeyDown( KeyCode.e ) ) {
SceneManagement.LoadScene( "Village", LoadSceneMode.Single );
}
If you don't want the player to be teleporting while the key is being held down, don't use Input.GetKey
. Use Input.GetKeyDown
or Input.GetKeyUp
.
Here are some references:
Input.GetKey - Returns true while the user holds down the key identified by name. Think auto fire.
Input.GetKeyDown - Returns true during the frame the user starts pressing down the key identified by name.
Input.GetKeyUp - Returns true during the frame the user releases the key identified by name.
Your answer
Follow this Question
Related Questions
Teleport Between Scenes 0 Answers
Help on how to code crouching? 0 Answers
Trigger detects player collision and play specific audio from array 1 Answer
Player Can Not get off platform 1 Answer
How do I switch scenes when player dies 2 Answers