- Home /
This should be simple : Input Button Question
Hi everyone
So this is what I try to do: enable / disable the movement of my plane; I setup 2 input buttons for this and proceeded next;
var EngineOn = false; var EngineOff = false;
function FixedUpdate () {
if(Input.GetButton("StartEngine")) { EngineOn = true; EngineOff = false;
rigidbody.AddRelativeForce(0, 0, normalizedSpeed * forwardForce); //.. and so on }
if(Input.GetButton("StopEngine"))
{ EngineOn = false; EngineOff= true;
rigidbody.AddRelativeForce(0, 0, 0); //.. and so on
}
Currently this works just fine with a twist - unless I keep the button pressed, the command turns off.
I don't know how offensive that is to decent programmers out there but this is how an artist sees the code :) What I try to do is enable the bit of code that does all the acrobatics with my gameObject and simply disable that bit of code when the engine is not running; So, in how many ways I am wrong with this approach?
Thanks in advance,
Answer by Mike 3 · Nov 27, 2010 at 01:10 PM
Use Input.GetButtonDown instead, otherwise it'll fire your conditional statements every frame the button is held down
After that, you'll want to Add the force in FixedUpdate instead of where you have it, e.g:
var EngineOn = false; var EngineOff = false;
function FixedUpdate () { if (EngineOn) rigidbody.AddRelativeForce(0, 0, normalizedSpeed * forwardForce); }
function Update() {
if(Input.GetButton("StartEngine"))
{
EngineOn = true;
EngineOff = false;
//.. and so on
}
if(Input.GetButton("StopEngine"))
{
EngineOn = false;
EngineOff= true;
}
}
Heh, I hit post too early and my internet died before I could fix it. Anyways points up at a nicer answer
This happens in FixedUpdate function maybe it should be moved to Update?
I love it when I learn, thank you $$anonymous$$ike.
Update is much better for the input checking where you check if a key was pressed (doesn't matter too much about when you're checking if it's held), FixedUpdate is better for adding forces