- Home /
C# - Keycode sequence doesn't really work
So I have this that someone showed me and it worked perfectly, until I wanted to test some stuff and experiment a little. I did this, which doesn't look wrong to me, but for some reason neither I can stop the speed up after I do the key sequence again after activating it, nor the Debug.Log works properly... Yes, I have spedUp set up already and yes I have speed set up already, this is inserted onto my PlayerController script so I can't include the entire script. Hopefully someone can fix this up for me because I tried so many things and it's still broken :(
public KeyCode[] sequence = new KeyCode[] { KeyCode.A, KeyCode.B, KeyCode.C };
private int sequenceIndex;
if (Input.GetKeyDown(sequence[sequenceIndex]))
{
if (++sequenceIndex == sequence.Length)
{
if (spedUp == false)
{
speed = 25;
spedUp = true;
Debug.Log("fast");
sequenceIndex = 0;
}
if (spedUp == true)
{
speed = 10;
spedUp = false;
Debug.Log("slow again");
sequenceIndex = 0;
}
}
}
You have 2 separate if blocks. If you enter the first one you set speedUp to false and always enter the second one too right after the first.
A boolean has only 2 possible values so just do
if (spedUp)
{
speed = 25;
spedUp = false;
Debug.Log("fast");
}
else
{
speed = 10;
spedUp = true;
Debug.Log("slow again");
}
sequenceIndex = 0;
Also make sure you don't get confused as you now set spedUp to false and set the speed to a higher value and vice versa.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to Use Key Combinations with the Control-Key? 2 Answers
Dynamic KeyCode Unity Exception 2 Answers