- Home /
While Loop Infinite... Rookie Mistake
You know, this is a rookie mistake. But, swallowing my pride... please help. The while loop I put in is looping infinitely.
I originally wanted it to be... while (movement buttons are pressed){ allow for sprinting; change boolean "sprintable" to TRUE; }
So... this is just a question; should I have made this an IF statement instead of a while? I'll test that to make sure as well. However, I want to know if it would work.
<code>#pragma strict
private var sprintable : boolean;
function Start () {
}
function Update () {
// Initiates the sprintable boolean, if movement buttons are pressed down. If these are pressed, the user is
// able to increase their speed by pressing shift alongside it.
// currently creates an infinite loop...
while(Input.GetKeyDown("w") || Input.GetKeyDown("a") || Input.GetKeyDown("s") || Input.GetKeyDown("d")){
sprintable = true;
Debug.Log('You are moving!');
}
}</code>
Thanks in advance.
Ah... fixed it. I used an IF statement ins$$anonymous$$d of while, and then nested the WHILE condition inside of the if. Easy fix, I guess ^^;
EDIT: Nope. Not fixed. God, I need practice...
Answer by KiraSensei · Dec 01, 2013 at 04:24 PM
Try this :
#pragma strict
private var sprintable : boolean;
function Start () {
}
function Update () {
// Initiates the sprintable boolean, if movement buttons are pressed down. If these are pressed, the user is
// able to increase their speed by pressing shift alongside it.
// currently creates an infinite loop...
if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
{
sprintable = true;
Debug.Log('You are moving!');
}
}
You also may use KeyCode.
To handle movement, you rarely need a "while" loop because the Update method is called at every frame.
EDIT : new code for several buttons (not tested) :
#pragma strict
private var sprintable : boolean;
function Start () {
}
function Update () {
if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
{
Debug.Log('You are moving!');
if (Input.GetKey("e"))
{
sprintable = true;
Debug.Log('And sprinting!');
}
}
}
That's so true, actually. But, now I've run into another problem...
When I try to initiate sprinting (by printing something in the Debug log, it doesn't work. I'm assu$$anonymous$$g it's because I can't use two keys at once...? I can post the code if you wish.
#pragma strict private var sprintable : boolean;
function Start () {
}
function Update () { // Initiates the sprintable boolean, if movement buttons are pressed down. If these are pressed, the user is // able to increase their speed by pressing shift alongside it. // solving... if(Input.Get$$anonymous$$eyDown("w") || Input.Get$$anonymous$$eyDown("a") || Input.Get$$anonymous$$eyDown("s") || Input.Get$$anonymous$$eyDown("d")){ sprintable = true; Debug.Log(sprintable);
if(sprintable && Input.Get$$anonymous$$eyDown("w") || Input.Get$$anonymous$$eyDown("a") || Input.Get$$anonymous$$eyDown("s") || Input.Get$$anonymous$$eyDown("d")){
if(Input.Get$$anonymous$$eyDown("e")){
Debug.Log("You are sprinting...");
}
}
}
else if(Input.Get$$anonymous$$eyUp("w") || Input.Get$$anonymous$$eyUp("a") || Input.Get$$anonymous$$eyUp("s") || Input.Get$$anonymous$$eyUp("d")){
sprintable = false;
Debug.Log(sprintable);
}
}
This is the code...
Don't you need to keep the button down to move your character ?
I edited my answer to add the multi buttons, not tested, so there may be some problems, just tell me !
since you reset the nb$$anonymous$$oveButtons every update, so every frame, you'll never sprint in just one direction.