- Home /
Best use of GetButton
Hello,
What is the best use to get all button states, my code now is:
if(!Input.GetButtonUp("Left"))
{
if(Input.GetButton("Left"))
{
log("Button Hold");
if(Input.GetButtonDown("Left"))
{
log("Button Down");
}
}
} else log("Button Up");
Usage is: User Pressed Button Down => an "object" is selected, if button goes up, it is still selected. While holding mouse down, object can be moved.
Other solution what I thought of to seperate it in three seperate if statements.
So what is best use-case ? Thanks in advance.
Answer by Ebil · May 24, 2013 at 07:54 PM
Well looks like I found a solution, nothing else had worked for me. Here my code, of course if someone uses it the functions has to be replaced. What it does : if object clicked, marked red as long as clicked. If button is not clicked anymore, object is yellow "still selected", if anything else is clicked than the desired object, selection is revoked. Correct layers has to be set to objects that are clickable.
if(Input.GetButton("Left"))
{
Physics.Raycast( Camera.main.ScreenPointToRay( Input.mousePosition ), out hitInfo,100,(1<<8) + (1<<10));
if(hitInfo.collider != null)
{
log(hitInfo.collider.gameObject.name);
currentObject = hitInfo.collider.gameObject;
currentObjectScene = currentObject.GetComponent<SceneObject>();
currentObjectScene.setIsActive(true);
log("Set Object Active");
}
else
if(currentObjectScene != null)
{
if(currentObjectScene.getIsSelected())
currentObjectScene.reset(false,false);
}
}
if(Input.GetButtonUp("Left"))
{
if((currentObjectScene != null) && currentObjectScene.getIsActive())
{
currentObjectScene.setIsActive(false);
currentObjectScene.setIsSelected(true);
log("Set Object selected but not active anymore");
}
}
Answer by Tomer-Barkan · May 23, 2013 at 09:07 PM
GetButton()
will handle all the cases i think... so whenever the button is pressed, make sure the object is selected, and move it.
if (Input.GetButton("Left")) {
if (!selected)
selectObject();
moveObject();
}
I should have been more precise, there has are actions that are also used by all methods, i think with just getbutton it isnt the best use-case to solve this. Thanks for your answere.
Answer by Graham-Dunnett · May 23, 2013 at 09:09 PM
if (holding)
log("Button hold");
if (Input.GetButtonDown("Left")){
holding = true;
log("Button down");
}
if (Input.GetButtonUp("Left")){
holding = false;
log("Button up");
}
Answer by darthbator · May 23, 2013 at 10:11 PM
GetButton will register as true as long as the button is done. IMO the only uses for the other cases is if you want an action only ONCE on a button press (OnButtonDown) or if you want to hook into the use releasing the button. Outside of those cases I use normal GetButton for everything.
and thats exactly what i have to do, as i write action happens if button is pressed down, i just forgot there are also actions if its moved up (object still selected but not active anymore), there are also actions in GetButton that the others are sharing.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How do I set up my players controller script (How do I change the controls used to move and look) 2 Answers
CharacterController unexpectedly moving up 0 Answers
Script behaves differently when using a pad 1 Answer
What's the best alternative to Character Controller? 2 Answers