Search code examples
mobileunity-game-engineunityscript

Pressing more than one GUI element at a time in Unity


I'm developing controls for a mobile platform; my GUI takes up the left, center, and right thirds of the screen. When the player wants to jump, they press the center button. My problem is that it seems that only one GUI element is being allowed to be pressed at once (like the device has only one mouse cursor that moves) rather than me being allowed to jump and go right at the same time. My code:

// JavaScript
#pragma strict

static var guiLeft : int = 0;
static var guiRight : int = 0;
static var guiJump : int = 0;

function OnGUI () {

// leftarrow
if (GUI.RepeatButton (Rect (0,0,Screen.width/3,Screen.height), "", GUIStyle.none)) {
    guiLeft = 1;

    }
    else{
    guiLeft = 0;
    }


// right
if (GUI.RepeatButton (Rect (Screen.width-Screen.width/3,0,Screen.width/3,Screen.height), "", GUIStyle.none)) {
    guiRight = 1;
    }
    else{
    guiRight = 0;
    }

            // jump
if (GUI.RepeatButton (Rect (Screen.width/3,0,(Screen.width/3)+2,Screen.height), "", GUIStyle.none)) {
    guiJump = 1;
    }
    else{
    guiJump = 0;
    }

}


Solution

  • The Unity GUI only supports one inputsource at a time (for example the left mouse button, or the right mouse button). The correct way to tackle your problem would be to use Input.touches. That'd indeed mean you'll have to change a lot of things in your game.