Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by hemik · Aug 28, 2013 at 05:07 PM · androidjoystickmultitouch

Unity 4, Multitouch/FingerId and Android

I have an issue with creating 2 joysticks to control my game. There's a left and a right joystick, both have a little dot in the middle that's supposed to follow player's finger. It does work, but only for 1 joystick (either one). Once I try to control both at the same time the little dot from a joystick I used second follows the finger I pressed down first on the first joystick. From what I could tell the issue lies in the touch.fingerId function. I read somewhere that it was broken in Unity 3.X.X, was it fixed in Unity 4?

I could write it without the use of FingerId, but that would limit joystick's functionality (they would only work when the finger is directly on them, while the use of fingerId should allow me to track the movement outside of the joystick's graphic and have it work properly, the only requirement being that the touch began inside the graphic). Here's my code:

 using UnityEngine;
 
 using System.Collections;
 
 public class Joysticks : MonoBehaviour {
 
     PlayerStats playerStats;
     int fingerId;
     bool stickActive;
     GameObject attachedButton;
     
     void Start () {
         playerStats = GameObject.Find("Player").GetComponent<PlayerStats>();
         switch(this.transform.name){ //CHECKS WHICH JOYSTICK IT IS AND ASSIGNS VALUES ACCORDINGLY

             case "LeftStick":
                 transform.localPosition = leftStick;
                 attachedButton = transform.parent.transform.FindChild("LeftStickButton").transform.gameObject;
                 attachedButton.transform.position = transform.position;
                 break;

             case "RightStick":
                    transform.localPosition = rightStick;
                 attachedButton = transform.parent.transform.FindChild("RightStickButton").transform.gameObject;
                 attachedButton.transform.position = transform.position;
                 break;
         }
     
     }
     
     void Update () {
         if (stickActive){    
             foreach (Touch touch in Input.touches){
                 if(touch.fingerId==fingerId){
                     attachedButton.transform.localPosition = touch.position;
                 }
             }
             
             
             
         }
         
     }
     
     void OnPress(){ //THIS IS CALLED WHENEVER ONE OF THE JOYSTICKS IS TOUCHED AND TRIGGERS STUFF IN UPDATE()
         stickActive = !stickActive;
         
         if (stickActive){
         foreach (Touch touch in Input.touches){
             if(Vector2.Distance(touch.position, transform.position)<150 && touch.phase == TouchPhase.Began){
                 fingerId = touch.fingerId;
                 
             }
         }
             
         } else {
         attachedButton.transform.position = transform.position;    //RESETS DOT'S POSITION WHEN 
         }
     }
 }
 
Comment
Add comment
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

· Add your reply
  • Sort: 
avatar image
0

Answer by hemik · Aug 29, 2013 at 06:22 AM

Alright, I solved it, I had to change transform.position into localPosition in the Vector2.Distance check

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Andriod Multitouch with joystick 1 Answer

How to make first person game with joysticks? 0 Answers

Need to Put buttons on Iphone/Android game 1 Answer

Joystick won't move in Unity 0 Answers

How to use the joystick.js in unity3d?? 0 Answers


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