- Home /
Unity 5 Cross Platform Joystick position
Hi all, I'm developing a simple arcade game in 2d for which i need a Joystick, I've read some forums and watched some tutorials on how to use it, however for Unity 5's standard assets>CrossPlatformInput -I imported them from Assets>Import Package>CrossPlatformInput- I can seem to figure how to get the position of the Joystick to make my ship move around in the position towards the joystick is move.
My problem is, there is no 'Position' variable in the Joystick script for this version, so I have no idea how to actually get it. pardon me if the question seems dumb, I'm pretty much a beginner.
Also some details, I'm programming every script with JavaScript, and the CrossPlatformInput scripts are C#.
Answer by legion_44 · Aug 08, 2015 at 02:11 PM
Just use CrossPlatformInputManager.GetAxis("Horizontal" or "Vertical"); (you need to import UnityStandardAssets.CrossPlatformInput namespace)
Thank you so much!, it was exactly what I was looking for, although I don't know exactly how does in know that i'm trying to get the joystick's axis?, i mean what if there's other joystick on the scene?, anyway I will post my piece of code as another answer. can't thank you enough, I was stuck for days!
@valecarlos On the Joystick script in the inspector you can set the vertical and horizontal axes names so if you have two sticks you can name axes on first like "walk_horizontal" and "walk_vertical" and on other "look_horizontal" and "look_vertical" then you use these names in the script.
Answer by valecarlos · Aug 08, 2015 at 04:04 PM
thanks to @fapwel here is my code: the ship is facing to the right hand side of the screen and the language is Javascript, although the Assets are C#, since in Javascript we don't use namespaces i used the whole path when declaring the variables
var joystick: UnityStandardAssets.CrossPlatformInput.Joystick; function FixedUpdate () { var joystickHorizontal = UnityStandardAssets.CrossPlatformInput.CrossPlatformInputManager.GetAxis("Horizontal"); var joystickVertical = UnityStandardAssets.CrossPlatformInput.CrossPlatformInputManager.GetAxis("Vertical"); if (joystickHorizontal != 0 && joystickVertical != 0) { var angle = Mathf.Atan2(joystickVertical , joystickHorizontal ) * Mathf.Rad2Deg; transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); }
the joystick is dragged from the Hierarchy to the variable joystick in the inspector
hope it helps people with the same problem!
Your answer

Follow this Question
Related Questions
Joystick Problems 1 Answer
C# Clamp joystick rotation 0 Answers
Why does pressing joystick button 0 also trigger joystick button 1? 1 Answer
late response mobile joystick crossPlatformInput 1 Answer
stay the rotation in character 1 Answer