- Home /
Translate doesnt work inside IF statement, but does outside.
Hi I have a code for my player to move if you push two buttons. If both statements are true (both buttons are pushed). then the player should move. However it doesn't. the weird thing is that it does when it's outside the IF-statement.
// Update is called once per frame
void FixedUpdate () {
// the player does move when this is uncommented:
//transform.Translate(0f, -1f * Time.deltaTime, 0f);
if (Button1down == 1&& Button2down == 1)
{
//player must move
transform.Translate(0f, -1f * Time.deltaTime, 0f);
}
}
is button1down a boolean? if yes then it should be if(button1down == true && button2down == true) {... } or short if(button1down&&button2down) {... }
thanks, but no I made it as an integer. and by the way if I put in print("lets see if this works..");
in the IF statement it does show up. but the transform doesnt work...
// the player does move when this is uncommented: //transform.Translate(0f, -1f * Time.deltaTime, 0f);
if (Button1down == 1 && Button2down == 1)
{
//player must move
transform.Translate(0f, -1f * Time.deltaTime, 0f);
}
} maybe try adding a space b/w the button1down == 1 && ins$$anonymous$$d of 1&& maybe that is causing problem
okay, and by the way. I noticed when I check the Button1- and Button2down manually in the inspector, it does work.
okay, so with the script applied to an empty gameobject it will check the Buttondown. but not on my player. Does anyone knows what might be the case? I am new at this, so it might be something very obvious.
Answer by anurag7991 · Jul 28, 2018 at 12:01 PM
void FixedUpdate () {
// the player does move when this is uncommented:
//transform.Translate(0f, -1f * Time.deltaTime, 0f);
if (Button1down == 1 && Button2down == 1)
{
//player must move
transform.Translate(0f, -1f * Time.deltaTime, 0f);
}
}
maybe try adding a space b/w the button1down == 1 && instead of 1&& maybe that is causing problem
thank you for your answer. That was a sloppiness from my part to copy my code to here, but not the problem.
Answer by Nebukam · Jul 28, 2018 at 03:24 PM
Make sure Button1down & Button2down aren't == 1 for one frame only, the log inside the if may be misleading as it will trigger once, and so will the Translate, but given you're using Time.deltaTime, for the duration of one frame (e.g 0.001 something) the Translate won't be nitceable. Check the transform in the editor while running play mode, it's highly likely that your if state is only valid once.
thank you for you answer, but I think it does run for multiple frames. I have a comment in the IF loop saying "player moves". I added a screenshot, where you can see it pushes out in an endless stream. I am just started with Unity, so I don't know for sure.
what I think is weird, is that like I said the player does move if the code is out the IF loop, and not if its in. Although it (seems) it will push a normal print in the IF loop just fine.