Problems with if-statements (C#)
Hello. I am making a game, my first proper game, and I have some problems. The player is an object that can change properties and material when a key is pressed.
It works fine enough, when I just have one double-if statement - even though it doesn't detect every key-hit, sometimes I have to press the key twice (I don't know why). But when I have one double-if statement for each playerstate (true/false), it doesn't work. This is part of my script:
void FixedUpdate (){
if (standardPlayer == true) {
if (Input.GetKeyDown(KeyCode.Q)){
standardPlayer = false;
Renderer rend = GetComponent<Renderer>(); rend.material = materials[1];
}
}
if (standardPlayer == false) {
if (Input.GetKeyDown (KeyCode.Q)) {
standardPlayer = true;
Renderer rend = GetComponent<Renderer> (); rend.material = materials[0];
}
}
}
The full class script can be found here: http://pastebin.com/1KXkKfNb
Any idea how I can fix it, so the player can toggle between the two states when he so desires? Also, is it best to place it in update or fixedUpdate?
Thanks in advance
Also, I tried using and (&&), ins$$anonymous$$d of having a second if-statement inside the first, but that didn't work either.
Just if anyone was wondering about the weird setup, I just tried a lot of things to make it work, but it won't
Answer by Cepheid · Apr 18, 2016 at 04:15 PM
Hi there @Chrras
When it comes to checking for input from the user it's best to place it within the Update() method rather than the FixedUpdate() method as FixedUpdate is only called every physics step and so the user key presses will sometimes be missed, hence why you sometimes have to hit the key twice for it to register. Also, FixedUpdate should only be used for physics calculations or mathematical algorithms that require a fixed frame rate.
As for the if statements. When you are comparing something to be either true or false you can make use of the "else" keyword. This keyword will cause the block to of code to be executed if the first conditional statement fails. So for example, consult below:
void Update ()
{
if (standardPlayer == true)
{
if (Input.GetKeyDown(KeyCode.Q))
{
standardPlayer = false;
Renderer rend = GetComponent<Renderer>(); rend.material = materials[1];
}
}
else
{
if (Input.GetKeyDown (KeyCode.Q))
{
standardPlayer = true;
Renderer rend = GetComponent<Renderer> (); rend.material = materials[0];
}
}
}
The code above will now check to see if standardPlayer is true. If not, it will then check to see if standardPlayer is false. Now, when you press the q key it will change the material and the standardPlayer variable so that next time, the other condition will become true.
If you were to not do this, then every time you press the Q key, it will evaluate both conditions immediately as it would check the first if statement, and then it would proceed to check the next if statement as well regardless of the outcome from the first, which by this point will have become false due to the first one and thus it looks like the material never changed at all, when it in fact did, it just changed too fast to notice.
I'm sorry if this explanation got lengthy, if you need further information or this didn't solve your problem feel free to let me know. I hope this helps! :)
Just to simplify the code down, maybe making it easier to read...
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q))
{
if (standardPlayer)
{
standardPlayer = false;
Renderer rend = GetComponent<Renderer>(); rend.material = materials[1];
}
else {
standardPlayer = true;
Renderer rend = GetComponent<Renderer>(); rend.material = materials[0];
}
}
}
Thanks a lot! That worked perfectly. I should really have asked the question in here before, since I have used several hours, trying to get it to work :)
I really appreciative your help, and your explanation was very understandable :)