x


Input trick question

I currently have my character moving with WASD. I have a GUI setup that I want to mimic the WASD movement. I was wondering if it were possible to have a function say if GUI Button pushed, then act as if Input.GetKeyDown("x") = true.

I'd like to do this because it's an easier way to do what I want, plus I'd just like to know :)

more ▼

asked Jul 25 '11 at 07:52 PM

zachypin gravatar image

zachypin
71 35 38 38

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

3 answers: sort newest

I have an on-screen WASD control that lets you push the buttons as though they were keys. I don't know if it's the best way, but what I did was use GUI.RepeatButton's and modified the MouseLook and FPSWalker scripts to have a new var like 'doVert' and 'doHorz' to accept these as though they were Inputs. A fragment:

if (ml == null)
    ml = Camera.main.GetComponent(MouseLook);
if (GUI.RepeatButton  (Rect (Screen.width - left, Screen.height - top, 44, 44), laTexture))
    if (ml)
       ml.doHorz = -.10f;
more ▼

answered Jul 25 '11 at 08:04 PM

DaveA gravatar image

DaveA
26.5k 151 171 256

looks like the closest I'll get to what I'm thinking

Jul 26 '11 at 04:37 PM zachypin
(comments are locked)
10|3000 characters needed characters left

You can have a function named "W" that when you press the W key, do that function. Then you can have a GUI button that if it is pressed, do that function.

more ▼

answered Jul 26 '11 at 01:53 PM

ConfinedDarkness gravatar image

ConfinedDarkness
81 3 3 4

yeah I know that. wanted to see if i could make a sort of hierarchy though so I don't have to do that. - push 'B' - Acts like you pushed 'A' - Pushing 'A' launches a function

Jul 26 '11 at 04:37 PM zachypin

Oh ok. Sorry i don't think that is possible.

Jul 26 '11 at 05:06 PM ConfinedDarkness

ooo welll. Would be a neat little function, but there are definitely a million ways around it, like the answer above.

Jul 26 '11 at 07:42 PM zachypin
(comments are locked)
10|3000 characters needed characters left

Have a Vector2 variable that is changed depending on input. Then make the position of your GUI element be that variable.

eg:

var guiPosition : Vector2;

function Update () {

guiPosition.x += Input.GetAxis("Horizontal"); guiPosition.y += Input.GetAxis("Vertical");

}

function OnGUI () {

GUI.Box(Rect(guiPosition.x, guiPosition.y, 100, 100), "I am a box!");

}

more ▼

answered Jul 25 '11 at 08:03 PM

ConfinedDarkness gravatar image

ConfinedDarkness
81 3 3 4

I have to admit that the question is not that clear at the start but near the end he quite clearly says he want to use a GUI.Button to emulate a key-down so he can move his char with the GUI-buttons. Your solution will move a gui element with WASD which is kinda the opposite :D

Jul 25 '11 at 10:11 PM Bunny83

Oh! Sorry I miss-read. I thought he wanted to move the GUI. I guess his question has been answered by dave.

Jul 25 '11 at 10:38 PM ConfinedDarkness

I was able to get my program working, but still don't have a direct answer to the question. I just didn't know if Unity supported a function that would say "Act like I just pushed 'a' ". So if I have WASD setup in code, instead of adding the arrow keys to the code I could just write code that when pushing the arrow keys the game is taking it as input from WASD.

Jul 26 '11 at 01:12 PM zachypin
(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:

x3695
x1371
x956
x36

asked: Jul 25 '11 at 07:52 PM

Seen: 940 times

Last Updated: Jul 26 '11 at 07:42 PM