cedart
August 19, 2015, 8:48pm
1
Hello,
I would like to do a simple thing, but I can’t.
I want when the player push a button, he take an object. And when he push the same button, he put down the object.
if(Input.GetButtonDown("Fire3") && haveObject == false){
haveObject = true;
}
if(Input.GetButtonDown("Fire3") && haveObject == true){
haveObject = false;
}
if(haveObject == true){
portableObject.transform.position = transform.position;
}
But when I push the button, he take the object and put it immediatly.
I think that’s because the button is still pushed for the second condition.
Someone have a solution pleaase ?
Thanks in advance !
Cédric (french man who don’t speak english good )
Mai oui! The value of Input.GetButtonDown() is not going to change between one line and the next. In fact, it will only change on the next frame.
Try this.
if (Input.GetButtonDown("Fire3")) {
haveObject = !haveObject;
}
if (haveObject){
portableObject.transform.position = transform.position;
}
cedart
August 19, 2015, 9:47pm
3
Nice idea thanks. Yes I’m not yet used to thinking with frames
So finally i can just write an else if instead of the second if.
Thank you so much
Whenever you face an issue like this, you should work through your code line and by line and analyze the logic.
if a button is down and have object is false, set have object to true
if a button is down and have object is true, set have object to false
if have object is true, move the object
The error in logic should jump right out at you.
cedart
August 19, 2015, 10:03pm
5
Yeh, but I use most often line by line compilation, like a “normal” program
In the update function, it’s a little bit different and I’ve to get used to