- Home /
How to use joystick Unity Mobile?
I'm brand new to the mobile arena. I've got a simple game where you roll around a ball using the arrow keys (by using AddForce to the sphere's rigidbody), but I want this game to be for android. So I've set the settings and all that, I have a joystick on the game screen, I just don't know how to use it to move the ball. What values do I access to find the position of the joystick? I've had a lot of trouble trying to figure this out... Thanks.
Can you be more specific about which joystick you're using? The one from the unity standard mobile package is not recommended for mobile because it uses GUITexture? If you got it from another source, look at the source code and find out the property to which the joystick output value is stored. If you can't understand any of this, probably you are new to Unity's way of coding.
I'm using the standard mobile package... if it's not recommended, what do you recommend I use?
Answer by jorjdboss · Jan 18, 2014 at 05:53 AM
First, create a joy stick base and a stick(use a cube or sprite, whatever), color them differently, make the stick 1/4th the size of the base. & parent the stick to the joy base.
Considering only the first touch, here's some pseudo code:
// attach this script to the joystick base
if(touchcount = 1)
{
desiredStickLocalPosition = touch position - joy base position
clamp desiredStickLocalPosition magnitude to joystick limits
stick.localposition = desiredStickLocalPosition
}
You may want to consider a package like Prime31's UIToolKit(free) or 2d toolkit(paid) to handle multi touch because you will end up needing it when you add other buttons.
I haven't coded this myself but I've tried make it understandable, hope it helps.
I'm just looking for the code that shows the current position of the joystick... aka joystick.position.x or something like that. But I will look into UITool$$anonymous$$it, thank you.
For the output you would want a normalised value(-1,1) for each axis.
output = stick.localposition / joystick limits
Your answer
Follow this Question
Related Questions
Rotate GameObject Z axis only relative to Joystick 0 Answers
Mobile joystick not moving player 0 Answers
How do I make on screen buttons in Android? 4 Answers
Porting simple game to android (Problem -> Controller) 0 Answers
GUI Joystick 1 Answer