Could someone help me with scripting a Trigger that when the player enters and then presses the E Key a new level is loaded. My script is JS at the moment.
pragma strict
var newScene : String;
function OnTriggerStay() { if(Input.GetKeyDown("E")){ Application.LoadLevel ("Chapter1"); } }
Answer by tperry1x · Nov 20, 2015 at 04:34 PM
Perhaps something like this is the simplest way I can think of doing it.
// attach to your player object
// the 'target' could be an empty cube gameobject that the player is inside of
var distance;
var target : Transform;
var activedistance = 11; // how close to the cube the player has to be
function Update () {
distance = Vector3.Distance(target.position, transform.position);
if(distance < activedistance){
if ( Input.GetKeyDown("e")) {
doEkeystuff (); // stuff you want to happen when the e key is pressed
}
}
}
function doEkeystuff (){
// stuff in here which happens
Debug.Log("you are in range and pressed the E key");
}
A unity example file attached if helpslink text
I tried this but it seams to not work I don't know what I'm doing wrong. I don't get any errors but when my player enters the trigger nothing happens. I have my scenes in the build setting and everything.
Your answer
Follow this Question
Related Questions
How do I make my game change levels after collecting all objects? 1 Answer
SceneManager.LoadScene () breaks my Trigger Colliders 0 Answers
Communicate between scenes with prefabs? 3 Answers
How to passing levels automatically in games? 0 Answers
Weird behavior when loading levels using triggers. 0 Answers