- Home /
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();
}
}*/
}
Your answer
Follow this Question
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