- Home /
How to emulate keyboard keys with a gui button?
Is it possible to emulate keyboard keys with a gui button? I want to move my character with a 4 gui buttons, but instead of re doing the input script, is there a way to map a gui button to simulate a keyboard key? Like if I were to press a button, then it would do what the "w" key does, and move forward.
It seems simple to do I just haven't been able to find a way to simulate a keyboard input.
Answer by aldonaletto · Jul 14, 2011 at 03:01 AM
I've not found any way to fool Unity and make it "think" a key was pressed, but you can create a function to substitute Input.GetAxis, for instance. Add the code below to the script where you read Input.GetAxis("axis"), and just remove the dot to transform the original function calls in InputGetAxis("axis"):
private var axisH: float = 0;
private var axisV: float = 0;
function InputGetAxis(axis: String): float {
var v = Input.GetAxis(axis);
if (Mathf.Abs(v) > 0.005) return v;
if (axis=="Horizontal") return axisH;
if (axis=="Vertical") return axisV;
}
function OnGUI(){
axisV = axisH = 0;
if (GUI.RepeatButton(Rect(50, 10, 40, 40), "W")) axisV = 1;
if (GUI.RepeatButton(Rect(50, 90, 40, 40), "S")) axisV = -1;
if (GUI.RepeatButton(Rect(10, 50, 40, 40), "A")) axisH = -1;
if (GUI.RepeatButton(Rect(90, 50, 40, 40), "D")) axisH = 1;
}
Use this InputGetAxis like the conventional Input.GetAxis, like below:
dirH = InputGetAxis("Horizontal");
dirV = InputGetAxis("Vertical");
...
Thank you very much, though I'm getting a parsing error hopefully I can get this working soon, appreciate it! Its just silly that theres not an easier way to emulate keys.
thanks, nice code. But I aks emulate rotation of mouse in de axis x in two button
var rotateSpeed = 20.0;
function Update () {
transform.Rotate(Vector3.down, Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime);
It's the same thing: copy the code above at the beginning of your script and remove the dot from Input.GetAxis:
transform.Rotate(Vector3.down, InputGetAxis("Horizontal") * rotateSpeed * Time.deltaTime);NOTE: there's a rule in Unity Answers: only use answers to answer to questions; in this case, you should have posted it as a comment in my answer (or as a new question). You may think: Why nobody told me that before?, and I would agree: there should exist some kind of omnipresent warning about this!
my god "Aldo Naletto" thank you so much, spent absolutely ages trying to get something on the lines of this working, <3
Answer by Raatopojat · Dec 05, 2014 at 11:07 PM
Hello, so here's my script for geared car on phone. I wonder why i am not getting any reaction when i try to press the up button. Am i even close to understand this example?
It'd be more than hundred rabbits if you can find me the answer!! :)
var enginePower=150.0;
var up : GUITexture;
var Magni : float;
private var axisH: float = 0;
private var axisV: float = 0;
function InputGetAxis(axis: String): float {
var v = Input.GetAxis(axis);
if (Mathf.Abs(v) > 0.005) return v;
if (axis=="Horizontal") return axisH;
if (axis=="Vertical") return axisV;
}
function Start(){
axisV = axisH = 0;
}
function Update () {
for (var touch : Touch in Input.touches){
if(touch.phase == TouchPhase.Stationary && up.HitTest (touch.position)){
if (Magni >= 0){
axisH = 1 * enginePower * Time.deltaTime * 600.0;
}
if (Magni > 50){
axisH = 1 * enginePower * Time.deltaTime * 250.0;
}
if (Magni > 100){
axisH = 1 * enginePower * Time.deltaTime * 100.0;
}
if (Magni > 110){
axisH = 1 * enginePower * Time.deltaTime * 80.0;
}
if (Magni > 130){
axisH= 1 * enginePower * Time.deltaTime * 30.0;
}
}
}
}