Search Unity

mapping keyborad and mouse to android on gui

Discussion in 'Scripting' started by mtwiscool, Jan 4, 2014.

  1. mtwiscool

    mtwiscool

    Joined:
    Jan 13, 2013
    Posts:
    25

    wsad for movement i want mapped onto the screen as displayed in the photo.
    lmb = left mouse button
    rmb = right mouse button
    a = attack
    circal is mouse for looking round
    mouse lock:
    http://pastebin.com/cYN1iHng

    the help will be vary welcome and people who help will get the ad free version free(android)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    The easiest way to do this is to make a list of rectangles and a list of what they map to.

    You need to already have a function that accepts keycodes in to control your game, then you can write some code to fabricate a static list of rects and keycodes.

    When you have this list of rectangles, in your Update() you can take the incoming Input.touches positions (remember to invert them on the Y axis!) and check against the screen rectangles.

    If you have a Touch that is TouchPhase.Begin, and it is within a particular rectangle in the list, then send the appropriate keycode into the keycode handler.

    It's a bit trickier for sustained touch, but it's the same idea: as long as you have TouchPhase.Stationary or TouchPhase.Moved, do the rect check and tell the keycode handler the input is still valid. Otherwise the key is released.

    The nice benefit of this is that when you are debugging, you can make an OnGUI() function that enumerates the above rectangles you have created, and makes little semi-transparent rects onscreen so you can verify where they are located. You can even light them up when the user touches them for additional feedback.

    Sorry I don't have time to write a sample right now, but this should get you started.

    Remember to create all your rects as percentages of the Screen.width and Screen.height in order to have them change size with different resolution devices.

    Kurt