- Home /
Left and Right Arrow Keypress for navigation between scenes.
Hi,
I need help urgently as i am new in unity3d.
i have 3 scenes, each scene has a left and right button.
I wanted this: If i am in scene1,and i press the right arrow button, it should do a Application.LoadLevel to scene 2. and if i press left arrow, i move back to previous scene.
But the strange thing is that, now with three scene setup and i go to scene 1 and when i press the right arrow, it moves me from Scene1 to scene2 to scene 3, without stopping at scene2. the left arrow bring me directly to scene 1 without stopping at scene2.
i tried forcing the variables KeypressedFw and KeypressedBck to false but seems does't work. please help.
here is the js for scene1, that i attached to my Main Camera.
function OnGUI() {
var keyPressedFw; var keyPressedBck;
keyPressedFw = false; keyPressedBck= false;
keyPressedFw = (Event.current.keyCode == KeyCode.RightArrow)? true : false; keyPressedBck= (Event.current.keyCode == KeyCode.LeftArrow)? true : false;
GUI.Button(Rect((Screen.width / 2) - 75, (Screen.height / 2) -50, 150, 100), "Scene 1"); GUI.Button(Rect((Screen.width / 2) - 150, (Screen.height / 2) -50, 50, 100), "Bck"); GUI.Button(Rect((Screen.width / 2) + 100, (Screen.height / 2) -50, 50, 100), "Fwds");
if (keyPressedBck == true) {
}
if (keyPressedFw == true) {
// yield WaitForSeconds(.5);
Application.LoadLevel ("scene2");
}
} //onGui
Peviously i was using The following code that allows me to even use the mouse to change scenes:
function OnGUI() {
var keyPressedFw = (Event.current.keyCode == KeyCode.RightArrow)? true : false; var keyPressedBck= (Event.current.keyCode == KeyCode.LeftArrow)? true : false;
GUI.Button(Rect((Screen.width / 2) - 75, (Screen.height / 2) -50, 150, 100), "Scene 1");
if (keyPressedBck || GUI.Button(Rect((Screen.width / 2) - 150, (Screen.height / 2) -50, 50, 100), "Bck")) { } if (keyPressedFw || GUI.Button(Rect((Screen.width / 2) + 100, (Screen.height / 2) -50, 50, 100), "Fwds")) { // yield WaitForSeconds(.5); Application.LoadLevel ("scene2"); } }
please somebody help.
regards nigel
When you post code, highlight it all and press the button with 0's and 1's on it, and it will format nicely so it's easier to read.
Being new to Unity doesn't make your problems any more urgent - it just means you are likely to have more of them. What's with the empty ifs? I don't see anything about loading scene 3 anywhere in this code. Assu$$anonymous$$g only the yield is commented out in the second code, the only change is moving the buttons out of the if statements. Is this what you meant to post?
Answer by skovacs1 · Aug 24, 2010 at 04:49 AM
OnGUI may be called several times per frame. If you load another scene, its OnGUI will be called and if the code is about the same, the event will be fired quickly enough that it will register the arrow key and load the next scene. Try adding a timer or wait of some kind before you start checking for the user input.
Your answer
Follow this Question
Related Questions
How to use explicit navigation without arrow keys? 0 Answers
proximity, keypress and mouse lock help please :) 1 Answer
Basic animation key press 0 Answers
How To Activate/Deactivate Image Effect On Keypress? 3 Answers
Need to press key twice to react 1 Answer