In my current project (Web Player / APK – works on Nexus 7 and Galaxy Nexus), I have my user clicking and dragging objects from the world onto a spot on the GUI (I manage this through 3 cameras and changing layers, but that’s working just fine, right now). It works -almost- flawlessly with mouse input (the part that doesn’t work is based on me using global coords on something that’s also dependent on it’s local position, but I can’t get using local coords to work properly), but doesn’t work how I expect it to with touch controls.
The camera is facing down onto the object, so the object is moving on X and Z, not X and Y. With touch input, that part works just fine, but the object also drops from (0,0,0) to something like (0,-0.98257348239,0) and the Y is constantly changing.
This leads me to believe that I’ve been doing my mouse controller wrong this whole time, but it’s just somehow worked the way I expected.
Anyway, here’s my code:
using UnityEngine;
using System.Collections;
using System;
bool startSet;
Hashtable startHash = new Hashtable();
Vector3 screenPoint;
#if (UNITY_ANDROID || UNITY_IPOD)
void Update(){
if(Input.touchCount > 0) {
if(Input.GetTouch(0).phase.Equals(TouchPhase.Began)){
ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if (Physics.Raycast(ray, out hit, 5)) {
if(!startSet) {
StartPosition = gameObject.transform.position;
startHash.Add("position",StartPosition);
startHash.Add("time",transitionTime);
startSet = true;
}
}
}
else if(Input.GetTouch(0).phase.Equals(TouchPhase.Moved)){
Vector3 screenPoint = Camera.main.WorldToScreenPoint (hit.transform.position);
Vector3 curScreenPoint = new Vector3(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y,screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
if(curPosition.y != StartPosition.y+0.1f)
curPosition.y = StartPosition.y+0.01f;
Hashtable newPosition = new Hashtable();
if(curPosition.y != StartPosition.y+0.1f)
curPosition.y = StartPosition.y+0.01f;
newPosition.Add("position",curPosition);
newPosition.Add("time",0f);
iTween.MoveTo(gameObject,newPosition);
}
else if(Input.GetTouch(0).phase.Equals(TouchPhase.Ended)) {
iTween.MoveTo(gameObject,startHash);
startHash.Clear();
startSet = false;
}
}
}
#else
void OnMouseDown(){
if(!startSet) {
StartPosition = gameObject.transform.position;
startHash.Add("position",StartPosition);
startHash.Add("time",transitionTime);
startSet = true;
}
}
void OnMouseDrag(){
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
Hashtable newPosition = new Hashtable();
if(curPosition.y != StartPosition.y+0.1f)
curPosition.y = StartPosition.y+0.01f;
newPosition.Add("position",curPosition);
newPosition.Add("time",0f);
iTween.MoveTo(gameObject,newPosition);
}
void OnMouseUp() {
iTween.MoveTo(gameObject,startHash);
startHash.Clear();
startSet = false;
}
#endif
I honestly don’t know what’s right or wrong with this anymore, all I know is that the mouse stuff seems to work the way I want it to, and the touch stuff doesn’t. I don’t care to fix the mouse stuff – I will if it’s wrong, of course, but don’t fix what ain’t broke – but the touch stuff is driving me crazy.
Also, this code is cleaned up (less comments, less jumping between scripts, no static variables, ect.) so I might be missing some variable declarations. Trust me, they’re there in the actual code.