- Home /
Set Input.GetKey to true by scripting
Can i fake the Input.GetKey to an GUI.RepeatButton by setting the Input of the Key to true when pushing the Button? I tried the following, but it doesn't work. Is there any possibility?
if( GUI.RepeatButton( new Rect( 30, 30, 50, 50 ), "right" ) )
{
Input.GetKey(KeyCode.RightArrow) = true;
Debug.Log(Input.GetKey(KeyCode.RightArrow);
}
Answer by Loius · Aug 22, 2013 at 01:17 AM
No.
Use flags:
if ( gui.button() ) wantRight = true;
...
if ( Input.GetKey(stuff) ) wantRight = true;
...
if ( wantRight ) {
// things
}
I found this: http://answers.unity3d.com/questions/375618/button-input.html But it doesn't work in C#. By implementing this, I only can go 'Forward', but not 'Backward'. It works, if i delete the 'else-phrase', but obviously the character then doesn't stop after clicking one of the buttons once. Why does it only work in one direction as long as I have the 'else-phrase' used? How can I solve this problem?
function OnGUI()
{
if ( GUI.RepeatButton( Rect( 10, 10, 100, 35 ), "Forward" ) )
{
vertInputGUI = 1.0;
}
else if ( GUI.RepeatButton( Rect( 10, 55, 100, 35 ), "Backward" ) )
{
vertInputGUI = -1.0;
}
else
{
vertInputGUI = 0.0;
}
}
I figured it out, i just have to place the 'else-phrase' in betweet of the two 'if-phrases' :) thanks anyway!
Your answer
Follow this Question
Related Questions
Can you drive a rigidbody 1 Answer
Only KeyPress once? 1 Answer
How to disable a keypress after a certain amount of time? 1 Answer
OnGUI/iPhone - cannot touch TWO+ buttons at the same time...? 3 Answers
gui.button to enable keypress 2 Answers