x


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.

more ▼

asked Jul 14 '11 at 01:41 AM

WillModelForFood gravatar image

WillModelForFood
46 10 11 15

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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

answered Jul 14 '11 at 03:01 AM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

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.

Jul 14 '11 at 03:32 AM WillModelForFood

Aldo Naletto thank you so much <3

Apr 01 '12 at 04:17 PM jesusluvsyooh

Ah, this was great! Thank you.

Oct 24 '12 at 04:49 PM Enkendu

Awesome the GUI.RepeatButton Handles many same problems!!

Dec 03 '12 at 03:17 PM behzad.robot

Thank you Aldo Naletto, thank you very much. :-) you saved my day. thanks a lot.

Dec 21 '12 at 06:21 AM DewDrop
(comments are locked)
10|3000 characters needed characters left

my god "Aldo Naletto" thank you so much, spent absolutely ages trying to get something on the lines of this working, <3

more ▼

answered Apr 01 '12 at 04:48 PM

jesusluvsyooh gravatar image

jesusluvsyooh
0

(comments are locked)
10|3000 characters needed characters left

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);
more ▼

answered Jul 18 '11 at 11:59 AM

ermenda gravatar image

ermenda
1 1

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!
Jul 18 '11 at 04:48 PM aldonaletto

sorry , it's my first reply.

Jul 18 '11 at 05:18 PM ermenda

don't mind, everybody makes this mistake at the first time!

Jul 18 '11 at 07:49 PM aldonaletto

is there any way to multiple key input at a time from virtual keys?

Jan 01 at 08:46 AM DewDrop
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x956
x790
x195
x21

asked: Jul 14 '11 at 01:41 AM

Seen: 2747 times

Last Updated: Jan 01 at 08:46 AM