- Home /
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. :D
Answer by Luci85 · Jul 03, 2012 at 05:47 PM
The easiest way is to use the standard buttons: http://docs.unity3d.com/Documentation/ScriptReference/GUI.Button.html And script the functionality you want.
Answer by nastasache · Jul 03, 2012 at 10:17 AM
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.
lulian, thanks for the help, but how come i can't see the joystick when i put it on play? how can i set how big or small the joystick is and where it is on the screen? how can i set what would that joystick do?
i tried to look at the script of the joystick.js file then my $$anonymous$$d exploded on how long the codes was. if possible, could you help me out more? im still kinda lost.
Hello i'm interested in that start-method. But, what's the point of step "2" , .. create a plane and drag it at (0,0,0) ... ? thanks
Hello i'm interested in that start-method. But, what's the point of step "2" , .. create a plane and drag it at (0,0,0) ... ? thanks
Answer by Ouss · Jan 25, 2014 at 03:14 PM
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
Answer by Rohit-Joshi · Jul 16, 2017 at 07:05 AM
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
Your answer
Follow this Question
Related Questions
GUI Joystick 1 Answer
Mobile Joystick Problem 2 Answers
How to shoot using the right mobile Joystick? 2 Answers
Android C# JoyStick Movement 0 Answers
Making one directional virtual joystick OR smoothly transitioning between buttons 0 Answers