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 omegagriever · Dec 31, 2012 at 07:57 AM · androidiphonetouchjoystickmulti-touch

ITouch multi touch with joystick problem

hi,

I am having some issues with the multitouch in Unity iPhone/android. I want to apply the following script onto the screen like Heroes of League ,such that I can move my character with left finger and attack with right finger. But whenever I touch one finger, the other finger didn't work , it just think the 2nd finger is new position of my 1st finger.

here is my unity itouch.cs


 using UnityEngine;
 
 #if UNITY_IPHONE
     //Add empty class
     public class iTouch : MonoBehaviour 
     {
         static public void ResetTouch ()
         {
             return;
         }
 
         void Start()
         {
             iPhoneSettings.verticalOrientation = true;
             iPhoneSettings.screenCanDarken = true;
         }    
     }
 
 #else
     //Add iPhone hack
     public enum iPhoneTouchPhase
     {
         Began = 1,
         Moved = 2,
         Stationary = 3,
         Ended = 4,
         Canceled = 5
     }
     
     public struct iPhoneTouch
     {
         public int fingerId;
         public Vector2 position;
         public Vector2 positionDelta;
         public float timeDelta;
         public int tapCount;
         public iPhoneTouchPhase phase;
     }
     
     public class iPhoneSettings
     {
         static public bool verticalOrientation = true;
         static public bool screenCanDarken = true;
         static public string uniqueIdentifier = "PC";
         static public string name = "PC";
         static public string model = "PC";
         static public string systemName = "PC";
         static public string systemVersion = "PC";
     }
     
     public class iPhoneInput
     {
         static public iPhoneTouch[] touches;
         static public int touchCount;
         static public bool multiTouchEnabled;
         
         static public iPhoneTouch GetTouch (int index)    
         {
             return touches[0];
          
         }
     }
     
     public class iTouch : MonoBehaviour {
     
         //Add no code
         //-----------------------------------Public Variables------------------------------------
         
         //-----------------------------------Private Variables-----------------------------------
         private Vector2 pSpeed;
         private Vector3 pLastPos;
     
         //----------------------------------------Methods----------------------------------------
         static public void ResetTouch ()
         {
             iPhoneInput.touches[0].timeDelta = Time.time;
             iPhoneInput.touches[0].phase = iPhoneTouchPhase.Canceled;
             iPhoneInput.touchCount = 0;
         }
         
         //-------------------------------------Unity Methods-------------------------------------
         void Awake ()
         {
             //Setup input
             iPhoneInput.touches = new iPhoneTouch[1];
             iPhoneInput.touches[0] = new iPhoneTouch();
             iPhoneInput.touches[0].fingerId = 0;
             iPhoneInput.touches[0].position = (Vector2) Input.mousePosition;
             iPhoneInput.touches[0].positionDelta = new Vector2(0,0);
             iPhoneInput.touches[0].timeDelta = Time.time;
             iPhoneInput.touches[0].tapCount = 0;
             iPhoneInput.touches[0].phase = iPhoneTouchPhase.Canceled;
             iPhoneInput.touchCount = 0;
             iPhoneInput.multiTouchEnabled = false;
         }
         
         void Update ()
         {
             Vector2 CurMousePos = (Vector2) Input.mousePosition;
 
             switch (iPhoneInput.touches[0].phase)
             {
                 case iPhoneTouchPhase.Canceled: //Default state
                     if (Input.GetMouseButtonDown(0) == true)
                     {
                         //Debug.Log("Set Began");
                         iPhoneInput.touches[0].fingerId = 0;
                         iPhoneInput.touches[0].position = CurMousePos;
                         iPhoneInput.touches[0].positionDelta = new Vector2(0,0);
                         iPhoneInput.touches[0].timeDelta = Time.time;
                         iPhoneInput.touches[0].tapCount = 0;
                         iPhoneInput.touches[0].phase = iPhoneTouchPhase.Began;
                         iPhoneInput.touchCount = 1;
                     }
                     break;
                     
                 case iPhoneTouchPhase.Began:  //Mouse down
                     //Debug.Log("Set Stationary");
                     iPhoneInput.touches[0].timeDelta = Time.time;
                     iPhoneInput.touches[0].phase = iPhoneTouchPhase.Stationary;
                     break;
                     
                 case iPhoneTouchPhase.Stationary:
                     //Check for motion
                     if (iPhoneInput.touches[0].position != CurMousePos)
                     {
                         //Debug.Log("Set Moved");
                         iPhoneInput.touches[0].positionDelta.x = CurMousePos.x - iPhoneInput.touches[0].position.x;
                         iPhoneInput.touches[0].positionDelta.y = CurMousePos.y - iPhoneInput.touches[0].position.y;
                         iPhoneInput.touches[0].position = CurMousePos;
                         iPhoneInput.touches[0].timeDelta = Time.time;
                         iPhoneInput.touches[0].phase  = iPhoneTouchPhase.Moved;
                     }
                     //Check for mouse up
                     if (Input.GetMouseButtonUp(0) == true)
                     {
                         //Debug.Log("Set Ended");
                         iPhoneInput.touches[0].timeDelta = Time.time;
                         iPhoneInput.touches[0].phase = iPhoneTouchPhase.Ended;
                     }
                     break;
                 
                 case iPhoneTouchPhase.Moved:
                     //Check for motion
                     if (iPhoneInput.touches[0].position != CurMousePos)
                     {
                         iPhoneInput.touches[0].positionDelta.x = CurMousePos.x - iPhoneInput.touches[0].position.x;
                         iPhoneInput.touches[0].positionDelta.y = CurMousePos.y - iPhoneInput.touches[0].position.y;
                         iPhoneInput.touches[0].position = CurMousePos;
                         iPhoneInput.touches[0].timeDelta = Time.time;
                     }
                     else
                     {
                         //Debug.Log("Stationary");
                         iPhoneInput.touches[0].positionDelta = new Vector2(0, 0);
                         iPhoneInput.touches[0].timeDelta = Time.time;
                         iPhoneInput.touches[0].phase = iPhoneTouchPhase.Stationary;
                     }
                     //Check for mouse up
                     if (Input.GetMouseButtonUp(0) == true)
                     {
                         //Debug.Log("Set Ended");
                         iPhoneInput.touches[0].timeDelta = Time.time;
                         iPhoneInput.touches[0].phase = iPhoneTouchPhase.Ended;
                     }
                     break;
                     
                 case iPhoneTouchPhase.Ended: //mouse up
                     //Debug.Log("Set Cancelled");
                     iPhoneInput.touches[0].timeDelta = Time.time;
                     iPhoneInput.touches[0].phase = iPhoneTouchPhase.Canceled;
                     iPhoneInput.touchCount = 0;
                     break;
             }
         }
     }
 
 #endif


joystick.cs


using UnityEngine; using System.Collections;

public class Joystick : MonoBehaviour { public float maxDistance; public SystemLogic systemLogic;

 public TowerButton[] towerButtonList;
 public SellButton sellButton;
 public UpgradeButton upgradeButton;
 
 public AttackController action;
 
 //private GUIQuadObj thisObj;
 //private Vector2 defaultPosition;
 private Vector2 joypadPosition;
 private Vector2 currentPosition;
 private bool onHold;
 private bool onBuild;
 //private int touchID;
 
 private Ray mouseRay;
 
 void Awake ()
 {
     //thisObj = (GUIQuadObj) gameObject.GetComponent(typeof(GUIQuadObj));
     //defaultPosition = thisObj.Location;
     onHold = false;
     onBuild = false;
     //touchID = -1;
 }
 
 /*void Update()
 {
     if (onHold && currentPosition.x > 480.0f)
     {
         onHold = false;
         //currentPosition = Vector2.zero;
         //joypadPosition = Vector2.zero;
     }
 }*/
 
 void onGUIUp (iPhoneTouch pTouch)
 {
     /*if (onHold)
     {
         GetComponent().size *= 0.01f;
     }*/
     
     if ((pTouch.position.x / Screen.width * 960.0f) <= 480.0f)
     {
         onHold = false;
     }
     //currentPosition = Vector2.zero;
     //joypadPosition = Vector2.zero;
 }
 
 void onGUIDown (iPhoneTouch pTouch)
 {
     bool menuSelected = false;
     if (!onHold)
     {
         //touchID = pTouch.fingerId;
         onBuild = false;
         sellButton.ResetPosition();
         upgradeButton.ResetPosition();
         for (int twrBt = 0; twrBt < towerButtonList.Length; twrBt++)
         {
             towerButtonList[twrBt].ResetPosition();
         }
         
         RaycastHit mouseHit;
         for (int stdNo = 0; stdNo < systemLogic.standList.Length; stdNo++)
         {
             mouseRay = Camera.main.ScreenPointToRay(pTouch.position);
             //Debug.DrawRay(mouseRay.origin, mouseRay.direction * 500.0f, Color.magenta);
             if (systemLogic.standList[stdNo].collider.Raycast(mouseRay, out mouseHit, 10.0f))
             {
                 if (systemLogic.standList[stdNo].OnUse())
                 {
                     sellButton.SetTarget(systemLogic.standList[stdNo]);
                     upgradeButton.SetTarget(systemLogic.standList[stdNo]);
                 }
                 else
                 {
                     onBuild = true;
                     for (int twrBt = 0; twrBt < towerButtonList.Length; twrBt++)
                     {
                         towerButtonList[twrBt].SetTarget(systemLogic.standList[stdNo]);
                     }
                 }
                 menuSelected = true;
                 break;
             }
         }
         
         if ((pTouch.position.x / Screen.width * 960.0f) <= 480.0f)
         {
             joypadPosition = new Vector2(pTouch.position.x / Screen.width * 960.0f, pTouch.position.y / Screen.height * 640.0f);
             currentPosition = new Vector2(pTouch.position.x / Screen.width * 960.0f, pTouch.position.y / Screen.height * 640.0f);
             onHold = true;
         }
     }
     
     if (!menuSelected && (pTouch.position.x / Screen.width * 960.0f) > 480.0f)
     {
         if (!action.animate.OnStun())
         {
             action.animate.Attack();
         }
     }
     //GetComponent().size *= 100.0f;
 }
 
 void onGUIMoved (iPhoneTouch pTouch)
 {
     if (onHold)
     {
         if ((pTouch.position.x / Screen.width * 960.0f) <= 480.0f)
         {
             if (!onBuild)
             {
                 sellButton.ResetPosition();
                 upgradeButton.ResetPosition();
             }
             else
             {
                 for (int twrBt = 0; twrBt < towerButtonList.Length; twrBt++)
                 {
                     towerButtonList[twrBt].ResetPosition();
                 }
             }
             
             currentPosition = new Vector2(pTouch.position.x / Screen.width * 960.0f, pTouch.position.y / Screen.height * 640.0f);
         }
         else
         {
             onHold = false;
         }
     }
 }
 
 public Vector2 GetValue()
 {
     if (onHold)
     {
         if ((currentPosition - joypadPosition).magnitude > maxDistance)
         {
             return ((joypadPosition + (currentPosition - joypadPosition).normalized * maxDistance) - joypadPosition) / maxDistance;
         }
         return (currentPosition - joypadPosition) / maxDistance;
     }
     
     return Vector2.zero;
 }

}


attack.cs


using UnityEngine; using System.Collections;

public class AttackButton : MonoBehaviour { //public AttackController action;

 private GUITextObj fpsText;
 private GUITextMgr textMgr;
 
 private float updateInterval;
 private float updateTimer;
 
 protected void Awake()
 {
     textMgr = (GUITextMgr) GameObject.Find("GUITextMgr").GetComponent(typeof(GUITextMgr));
     fpsText = textMgr.GetGUIText(gameObject.layer);
     fpsText.Text = (1 / Time.deltaTime).ToString();
     fpsText.Location = new Vector2(Screen.width * 0.5f, Screen.height);
     fpsText.GUIDepth = 1;
     fpsText.SetFormat(null, TextAlignment.Center, TextAnchor.UpperCenter, 1.34f, 4f);
     fpsText.TextMaterial = null;
     fpsText.Enabled = true;
     fpsText.TextMaterial.color = Color.blue;
     updateInterval = 0.5f;
     updateTimer = 0.0f;
 }
 
 protected void Update()
 {
     updateTimer += Time.deltaTime;
     
     if (updateTimer > updateInterval)
     {
         fpsText.Text = ((1 / Time.deltaTime)).ToString();
         updateTimer = 0.0f;
     }
 }
 
 /*void onGUIDown(iPhoneTouch pTouch)
 {
     if (!action.animate.OnStun())
     {
         action.animate.Attack();
     }
 }*/

}


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

8 People are following this question.

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

Related Questions

Move 2 objects at the same time 0 Answers

Question about Unity Touch Interface For Android 1 Answer

How can I make a thumbstick with this code? HELP 1 Answer

Swipe and Joystick Together on Mobile 0 Answers

Input.GetAxis("Vertical") on touch devices. 2 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