- Home /
iPad touch is offset! Unity2d.
Hi Everyone, My game was working fine until I upgraded to Unity 4.5.1. I have code that simply detects if an object is touched.
It works in the Unity Game window, it works on Android, but for some reason, the x value on the iPad is offset by 10 (after screentoworldpoint is run).
Is there a reason why upgrading Unity would change this? Was I doing it wrong to begin with? Either way, does anyone have any ideas on how to fix this?
Thanks, -Will
public int hasTouchedObject(GameObject objectToCheck)
{
//Debug.Log ("Camera Location: x: " + GameObject.Find("Main Camera").transform.position.x + ": y: " + GameObject.Find("Main Camera").transform.position.x + ": z: " + GameObject.Find("Main Camera").transform.position.z);
setTouches();
for (int x = 0; x < touchList.Count; x++)
{
Touch thisTouch = touchList[x];
//string touchphase = thisTouch.phase.ToString();
if (thisTouch.phase == TouchPhase.Began)
{
Debug.Log ("touch x: " + thisTouch.position.x + " touch y: " + thisTouch.position.x);
Vector3 wp = Camera.main.ScreenToWorldPoint(new Vector2(thisTouch.position.x, thisTouch.position.y));
Debug.Log ("Cam x: " + Camera.main.transform.position.x + " Cam y: " + Camera.main.transform.position.x + " Cam z: " + Camera.main.transform.position.z);
Vector2 touchPos = new Vector2(wp.x, wp.y);
Collider2D[] allpoints = Physics2D.OverlapPointAll(touchPos);
Debug.Log ("Touch point x: " + touchPos.x + " ::: Touch point y: " + touchPos.y);
for (int y = 0; y < allpoints.Length; y++)
{
//Debug.Log ("All Touch Point" + y);
if (objectToCheck.collider2D == allpoints[y])
{
Debug.Log ("FOUND!!!");
return y;
}
}
}
}
return -1;
}
Answer by Jonesy19 · Jul 10, 2014 at 05:40 PM
In case anyone else is running into the same issue, I figured out the problem. For whatever reason, when I was using the code "Screen.Orientation = Landscape.Left", that was throwing off the ScreenToWorldPoint coordinates into thinking it was portrait...Anyways, I removed ALL instances in my code where I was manually changing the screen orientation and allowed Unity to do it by default in the Player Settings inspector, and now it's working fine.
Answer by kboudai · Sep 15, 2014 at 11:17 PM
I got the same bug. You just need to update Unity >= 4.5.3.