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 Dekaren · Nov 06, 2014 at 07:59 AM · c#mobiletouchjoystickmultitouch

Android 2D multitouch joystick + buttons

I ran out of ideas about my code. I am trying to make a top-down 2D shooter for a school project and I want to control my character with a on-screen joystick and on-screen action buttons. I tried following the YT tutorials such as these:
Devin Curry's: https://www.youtube.com/watch?v=ZGvkaHHQD7c
Sebastian Lague's: https://www.youtube.com/watch?v=SrCUO46jcxk

...Both of them worked great but both had the same issue:
When I touch the button A, then touch the Joystick B And then, when I release my finger from the first button (A), The Joystick gets stuck and I get a :

UnityException: Index out of bounds.
Joystick.MoveJoyStick () (at Assets/Joystick.cs:43)
Joystick.OnTouchMoved (Int32 myTouch) (at Assets/Joystick.cs:29)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
TouchInput:Update() (at Assets/Scripts/TouchInput.cs:44)

Let me give you my code:

(TouchInput.cs) - placed on main camera, used for touch management, basicly Lague's code with a 2D raycast tweak

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class TouchInput : MonoBehaviour {
 
     public LayerMask touchInputMask;
 
     private List<GameObject> touchList = new List<GameObject>();
     private GameObject[] touchesOld;
 
     private RaycastHit2D hit;
 
     public static int currentTouch = 0;
 
 
     void Update () {
         if(Input.touchCount > 0) {
             touchesOld = new GameObject[touchList.Count];
             touchList.CopyTo(touchesOld);
             touchList.Clear();
 
             foreach (Touch touch in Input.touches) {
                 hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touch.position), Vector2.zero, touchInputMask);
                 if(hit.collider != null) {
                     
                     currentTouch = touch.fingerId;
 
                     oldTouch = currentTouch;
                     GameObject recipient = hit.transform.gameObject;
                     touchList.Add(recipient);
 
                     if(touch.phase == TouchPhase.Began) {
                         recipient.SendMessage("OnTouchBegan",currentTouch,SendMessageOptions.DontRequireReceiver);
                     }
                     if(touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) {
                         recipient.SendMessage("OnTouchMoved",currentTouch,SendMessageOptions.DontRequireReceiver);
                     }
                     if(touch.phase == TouchPhase.Ended) {
                         recipient.SendMessage("OnTouchEnded",currentTouch,SendMessageOptions.DontRequireReceiver);
                     }
                     if(touch.phase == TouchPhase.Canceled) {
                         recipient.SendMessage("OnTouchCanceled",currentTouch,SendMessageOptions.DontRequireReceiver);
                     }
                 }
             }
             foreach(GameObject i in touchesOld) {
                 if(!touchList.Contains(i)) {
                     i.SendMessage("OnTouchEnded",currentTouch,SendMessageOptions.DontRequireReceiver);
                 }
             }
         }
     }
 }

(Joystick.cs) - placed on empty game object that has a 2d collider, The Stick is a children of this game object

 using UnityEngine;
 using System.Collections;
 
 public class Joystick : MonoBehaviour {
 
     private Vector2 touchPos;
     private Vector2 snapPos; //joysticks original position
     [HideInInspector]
     public Vector2 dragDelta;
     public float maxdragDelta = 2f;
     public GameObject _stickGameObject;
     private Transform _stickTransform;
 
     private int Touch_id;
 
     void Start() {
         _stickTransform = _stickGameObject.transform;
         _stickGameObject.renderer.enabled = false;
         snapPos = transform.position;
     }
     
     void OnTouchBegan(int myTouch) {
         _stickGameObject.renderer.enabled = true;
 
         Touch_id = myTouch;
     }
     
     void OnTouchMoved(int myTouch) {
         _stickTransform.position = MoveJoyStick();
     }
     
     void OnTouchEnded(int myTouch) {
         _stickGameObject.renderer.enabled = false;
 
     }
     
     void OnTouchCanceled(int myTouch) {
         _stickTransform.position = MoveJoyStick();
     }
 
     Vector3 MoveJoyStick() {
         
         touchPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(Touch_id).position);
         
         Vector2 moveDir = new Vector3 (touchPos.x - snapPos.x, 
                                        touchPos.y - snapPos.y);
         
         if(moveDir.sqrMagnitude > maxdragDelta * maxdragDelta) {
             moveDir = Vector2.ClampMagnitude(moveDir, maxdragDelta);
         }
         moveDir += snapPos;
 
         return moveDir;
     }
 }


(Button.cs) - just a debug, when you touch it it switches color

 using UnityEngine;
 using System.Collections;
 
 public class Button : MonoBehaviour {
 
     public Color defaulColor;
     public Color selectedColor;
     private Material mat;
 
 
     void Start() {
         mat = renderer.material;
     }
 
     void OnTouchBegan(int myTouch) {
         mat.color = selectedColor;
     }
 
     void OnTouchMoved(int myTouch) {
         mat.color = selectedColor;
     }
 
     void OnTouchEnded(int myTouch) {
         mat.color = defaulColor;
     }
 
     void OnTouchCanceled(int myTouch) {
         mat.color = defaulColor;
     }
 }

PS. Sorry for excessive space use, but I really don't get the idea behind this problem. Just wanted you guys to have a broader look at the code.

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

How to stop second finger from manipulating position of gui joystick 1 Answer

Two or more touchs at same time? 0 Answers

Android: After the third touch all touches get cancelled 1 Answer

Prime 31 Touch Kit detecting if touch hit gui element 1 Answer

Issues with using multitouch and event trigger on 2 joysticks 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