How do I make on screen buttons in Android?

Hi, Technically I’m kinda new at Unity. How do I make 4 buttons to appear at the screen using JavaScript? the first 2 buttons will rotate my character left and right. the other 2 will be for shooting and stuff. May you please explain it to me as simple as possible? on what object will I attach the script?

I’m making an alien shooting game. I use the accelermometer to move my player.

and is it possible to use an image for that certain button. (i want to design how the button looks like in photoshop)

Please Help, Thank you very much and sorry for the bad English. :smiley:

The easiest way is to use the standard buttons: Unity - Scripting API: GUI.Button
And script the functionality you want.

Hi, roncel

The keyword is ‘Joystick’.
You have a demo scene under Standard assets(mobile), try to play with. The script you can use is named Joystick.js there.

You can use 2 rectangle “Joysticks” (with ‘Is touchpad’ option checked) for rotating and moving, and 2 “Joysticks” for shooting and stuff. I already have this system working properly,

Of course, you can use your own textures for the joysticks (transparent .png for example), to make these to look as buttons. The option to change texture it’s on joystick component options.

Note the joystick positions are relative to the screen (0 to 1 on vertical and horizontal). You can attach the joysticks to a new empty object. I use this kind of object positioned at (0,0,0) in the world space (the difference it’s important since the player is moving around and buttons must stay on the screen)

Above joysticks may be used with Javascript in your gameplay rules by capturing tapCount (clicks on) and position (accelerometer values) properties of each joystick.


You can see the joystick if you add texture for it.
Size an positions you can change using (in Inspector) Transform position and Pixel Inset values (these are the most important)

The basic steps:

  • Create a PNG texture having (for example) 64x64 pixels; make it semi-transparent; and copy it somewhere under Assets;
  • Create a plane and drag to the scene at (0,0,0)
  • Create a new empty object on the scene, at (0,0,0), rename it to TouchPad
  • Attach the Joystick.js script to the TouchPad object
  • Select TouchPad object in Hierarchy
  • In Inspector:
  • Change the “GuiTexture → Texture” to your file; your texture will appear on the screen (that’s point)
  • Play with “Transform → Position / Scale” and “Pixel Inset” to see how to change the position on the screen; changing position and size it’s related with your texture file size (this is reason I say about example 64x64)

Placing many touchpads require some exercise but finally will see are not so difficult.

You don’t need to read, understand and change all Joystick.js script to make touchpads working. Also, on Unity forum already exists some scripts to handle with accelerometer;

I don’t know how to upload here a simple package with all you need to see how few touchpads may be used togheter.

Anyway, if you have trouble with touchpads, the Luci85 solution (use standard buttons) it working also.

You can use the GUI.button to do so. Perhaps I have a problem while using joysticks prefab and button, while using the moving the joystick and press the button, the button doesn’t work

In my case, when I deploy the game to Android, the joystick is not visible, but its visible in Unity game mode. Please help me