- Home /
Button Input
I'm Brazilian and my english is basic.
I'm a beginner in Unity 3D and in Javascript.
I have a "GUI Button" on the screen. I want to make a action when this button is pressed. My project is a race game. When the player press this Button, the value of Vertical Input is '1'.
if (GUI.Button(Rect(1000,500,100,30),"Acelerar"))
{
//ACTION
}
How can I do it? Please, help me guys.
I'm trying to understand the problem. So you have Input.GetAxis("Vertical") , but when you press a GUI button, you want this value to change ? Really need an example to understand.
Say Input.GetAxis("Vertical") is returning 1. But if you press GUI button, this becomes -1 (like putting a car into reverse gear) ?
Or do you mean when you press GUI button, set the Input.GetAxis("Vertical") to stay at 1 ?
A want to 'convert' the Axis Inputs (Vertical and Horizontal) to display on the screen and compile for Android device. Then I need to put a button on the screen. I want to make a 'equal' between the button and the Vertical input, like the 'Equal' between Up Arrow and W.
So the buttons are doing the same job as Input.GetAxis("Vertical") , so you have 2 sets of inputs that do the same thing ? eg
Input.GetAxis("Vertical") (press W) to move forward
GUI.Button("Forward") also to move forward
Input.GetAxis("Vertical") (press S) to move backward
GUI.Button("Backward") also to move backward
is this correct?
But I want something like a link. The button action is linked to a vertical input.
In other situation, can i just add the vertical or horizontal axis on a gui to display? Does the click do an action?
Answer by AlucardJay · Jan 07, 2013 at 07:41 AM
I actually learned a new command while working on this! If you want a GUI.Button to detect if it is being held down, need to use GUI.RepeatButton (I don't use the Unity GUI).
This script demonstrates one way to achieve input from 2 sources. First, store 2 references for the inputs, one for the GetAxis and one for the GUI.Buttons :
private var vertInputGUI : float = 0.0;
private var vertInputKey : float = 0.0;
then add these 2 together, and make the result a value between -1 and +1 :
vertInputAll = Mathf.Clamp( vertInputKey + vertInputGUI, -1.0, 1.0 );
then just use this combined input value for your movement. Here is a test script. Create a new scene, create a cube, attach this script to the cube :
#pragma strict
public var speed : float = 5.0;
private var vertInputGUI : float = 0.0;
private var vertInputKey : float = 0.0;
private var vertInputAll : float = 0.0;
function Update()
{
vertInputKey = Input.GetAxis( "Vertical" );
vertInputAll = Mathf.Clamp( vertInputKey + vertInputGUI, -1.0, 1.0 );
transform.position += transform.forward * vertInputAll * speed * Time.deltaTime;
}
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;
}
}
Unity Scripting References :
Clamp : http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Clamp.html
GUI.RepeatButton : http://docs.unity3d.com/Documentation/ScriptReference/GUI.RepeatButton.html
Thank you very much, it works perfectly. I'm very grateful.
hey @gugu, if it's done, just click the "TIC$$anonymous$$" button
(round circle near thumbs)
only you can do it