• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by LordZephyr · Jul 10, 2014 at 02:31 AM · iostouchjoystickinvisible

How do I create an invisible joystick that appears anywhere on the iOS screen?

Hi, I'm creating a third person game and I want to make a single joystick controller but there are two distinctive things I want this joystick to do. I want it to be invisible until the player touches the screen. Then it becomes visible under the player's finger. I also want, and this is the hard part, to have the player be able to touch anywhere on the screen and have the joystick appear. I don't want it to be set in any particular location. Is there anywhere that I might be able to find this information or can anyone help me to create it? Thanks so much! Tom

Comment
HEATH3N
frufiGuild
drz01311

People who like this

3 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by MakeCodeNow · Jul 10, 2014 at 03:23 AM

Here's my version of the code that does that. Basic idea is wait for a touch to be down, use that as the center of the joystick, and then compute the joystick deflection as the difference between current finger pos and initial pos.

     protected class TrackedTouch
     {
         public Vector2 startPos;
         public Vector2 currentPos;
     }
 
     void _UpdateTouches()
     {
         int numTouches = Input.touchCount;
         for(int touchIndex = 0; touchIndex < numTouches; ++touchIndex)
         {
             Touch touch = Input.touches[touchIndex];
             if(touch.phase == TouchPhase.Began)
             {
                 Debug.Log("Touch " + touch.fingerId + "Started at : " + touch.position);
                 TrackedTouch newTouch = new TrackedTouch();
                 newTouch.startPos = touch.position;
                 newTouch.currentPos = touch.position;
                 _touches.Add(touch.fingerId, newTouch);
             }
             else if(touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended)
             {
                 Debug.Log("Touch " + touch.fingerId + "Ended at : " + touch.position);
                 _touches.Remove(touch.fingerId);
             }
             else
             {
                 TrackedTouch currentTouch;
                 if(_touches.TryGetValue(touch.fingerId, out currentTouch))
                 {
                     currentTouch.currentPos = touch.position;
                 }
             }
         }
 
     }
 
     protected Vector2 GetScreenJoystick(bool left)
     {
         foreach(TrackedTouch touch in _touches.Values)
         {
             float halfScreenWidth = Screen.width * 0.5f;
             if((left && touch.startPos.x < halfScreenWidth) ||
                (!left && touch.startPos.x > halfScreenWidth))
             {
                 Vector2 screenJoy = touch.currentPos - touch.startPos;
                 screenJoy.x = Mathf.Clamp(screenJoy.x / (halfScreenWidth * 0.5f * TouchScreenLookScale), -1f, 1f);
                     screenJoy.y = Mathf.Clamp(screenJoy.y / (Screen.height * 0.5f * TouchScreenLookScale), -1f, 1f);
                 return screenJoy;
                }
            }
            return Vector2.zero;
        }



Comment
LordZephyr
Xamarca
MSavioti
drz01311

People who like this

4 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LordZephyr · Jul 10, 2014 at 06:29 PM 0
Share

Thanks so much @MakeCodeNow for your fast and detailed response. I'm assuming it works because I am used to Unity's JavaScript and am still rather new at that. C# is something that I haven't used at all yet. You have done so much with this that I am very reluctant to ask if it would be possible for you to put it in JavaScript? I'm at a loss because I don't know how to troubleshoot C#. When I pasted your code into a C# file, I received 3 error codes: 1) Assets/MyScripts/InvisibleJoystickC.cs(1,17): error CS1527: Namespace elements cannot be explicitly declared as private, protected or protected internal: 2) Assets/MyScripts/InvisibleJoystickC.cs(7,6): error CS0116: A namespace can only contain types and namespace declarations: and 3) Assets/MyScripts/InvisibleJoystickC.cs(38,19): error CS0116: A namespace can only contain types and namespace declarations. I pasted it exactly as you have it above. I would really appreciate any further help you are willing to give me. Thanks so much for your time!!! Tom

avatar image MakeCodeNow · Jul 11, 2014 at 03:20 AM -1
Share

Yeah, that is code roughly extracted from a larger class. You'll need to do the work yourself if you want it ported to JavaScript. This is Unity Answers, not Unity Get Other People To Do Your Work For You.

avatar image NeverTrustShadows MakeCodeNow · Oct 18, 2021 at 10:59 AM 0
Share

Hilarious.

avatar image LordZephyr · Jul 11, 2014 at 04:04 AM 0
Share

Thanks for your help. I'll do my best. My game is 3/4 of the way done. This is all that's stopping me.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use the joystick to control the character 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Constrain touch joystick around a point 0 Answers

Multi touch not working - What's wrong with this code? 1 Answer

SmoothDamp touch look iOS Android 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges