- Home /
How to enable multiple button presses on a touchscreen virtual button control scheme
I am trying to develop a basic mobile game, and am having a little bit of a problem with the controls.
my initial keyboard-controlled code worked very smoothly and allowed the player to move along the x axis and shoot simultaneously. Left and right arrow keys moved, spacebar shoot.
void Update () {
myTransform.Translate(Vector3.right * playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime);
if (Input.GetKeyDown (KeyCode.Space)) {
Vector3 position = new Vector3(myTransform.position.x, myTransform.position.y + 1, myTransform.position.z);
Instantiate(laser, position, Quaternion.identity);
}
However, since moving to a mobile interface i've drawn virtual buttons on the screen and used those to control the player.
void OnGUI() {
if (GUI.Button (new Rect (425, 900, 200, 200), SpaceshipFire)) {
Vector3 position = new Vector3 (myTransform.position.x, myTransform.position.y + 1, myTransform.position.z);
//fire laser
Instantiate (laser, position, Quaternion.identity);
}
if (GUI.RepeatButton (new Rect (200, 900, 200, 200), SpaceshipRight)) {
myTransform.Translate (Vector3.right * playerSpeed * Time.deltaTime);
}
if (GUI.RepeatButton (new Rect (0, 900, 200, 200), SpaceshipLeft)) {
myTransform.Translate (Vector3.left * playerSpeed * Time.deltaTime);
}
The problem is:
Say you want to move left continuously while shooting. You would expect to hold the "Move Left" button down while tapping the "Shoot" button. In my old code, that was fine. In the new code, pressing the "shoot" button overrides you pressing the "move left" button. You cannot move and shoot.
I think I need to be looking at multitouch stuff, but all my searches lead to implementing multitouch gestures (like pinch to zoom).
Would just like to know where to look really.
Answer by Rostam24 · Sep 26, 2015 at 08:30 PM
I use UI Buttons in my game, this would work for you as well. Then in each UI Button, add a script that implements IPointerDownHandler to see if the button has been pressed, and build the player controls from there.