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.
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");
...
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;
}
}
}
}