- Home /
Using the standard virtual joysticks in c#
Forgive me if I'm asking a question that's been answered a million times, but I really can't find what I'm looking for. I am using the virtual joysticks in the Unity standard assets (mobile) package. I drag and dropped the single stick into my scene, I also dragged two touch pads onto my game for what I'll need them for. I test my game, and yay! the joysticks move around with my thumb just like I'd expect. Perfect, now I just have to find a way to see the value of that joystick to move my character with... Then I run into issues.
I can't seem to figure out how to find the values of the virtual stick. I found a few different ways on other forum questions, with people finding they didn't work and then people eventually offered up 3rd party virtual joysticks... but I just want to get the Unity one to work.
Now, I code in c#, could that possibly be an issue? I tried creating a public "Joystick" variable and then just dragging and dropping the joystick in the scene into it... but Joystick wasn't a variable type, and thus it wouldn't let me. I went into the player settings and changed the horizontal axis that moves my character to get movement from all joysticks, with no luck either.
If someone does know know how to find the values, could you also explain how to find the values of the touch pads so I don't run into this issue in the future when working on those?
Any help is greatly appreciated! Thanks!
Answer by GetColor · Nov 14, 2012 at 06:25 AM
Alright! I solved my problem.
First off, I didn't realize the unity virtual sticks were javascript. So after banging my head against google long enough, I found out that you can reference java objects as long as the script is compiled first... which is easy enough because the folder "standard assets" is compiled first - but "standard assets (mobile)" is NOT compiled first. So basically all I had to do was drag the "joystick" javascript from the standard assets mobile folder into the standard assets folder. SIMPLE! :D
Then it recognized the variable type "Joystick" in my c# script. So I created a public joystick variable, dragged and dropped my joystick prefab from my scene into it, and then was able to control my character using my joystickvariable.position.x in place of my normal input.getaxis("axis name") code.
Anyways! I've seen people have this same issue without a real conclusion, so I hope me posting this will help out another noob coder :P
Thanks, you've helped me finally get $$anonymous$$e to work. it's very considerate of you to put your solution here
Awesome work there, I too was getting nowhere fast with C# and the pre-fab joystick. Without you I would also be banging my head against Google.
Thank you so much! I spent half the night trying to figure this out!!
alter long google search finally I found this one.. thanks.
Answer by dogprez · Jan 17, 2014 at 07:45 PM
Just like Sussy said, but here is the reason why: http://docs.unity3d.com/Documentation/Manual/ScriptCompileOrderFolders.html
Answer by $$anonymous$$ · Feb 22, 2014 at 08:32 AM
You can always rename the Standard Assets(mobile) folder to Standard Assets and just copy all mobile assets under a subfolder named mobile.The important part is for the root folder to be called Standard Assets to be considered first pass material and avoid messing with the script compile order.
Be sure to do it in Unity so links to scripts on the prefabs are managed automatically and you don't have to fix them yourselves.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to make Lerpz Sidescroller work with iPhone Joystick? 1 Answer
MobileJoyStick not working on Unity Remote for iOS. 0 Answers
FPS Camera rotates on Z when using a joystick 0 Answers
Constrain touch joystick around a point 0 Answers