I'm unable to get a consistently accurate touch.position or hit.point using the following script. Touching the screen prints the following targetLocation to the console:
16.8, 6.1, 0.0
16.6, 6.1, 0.0
9.0, 2.9, 0.0 ← this one is where I was actually touching!
16.4, 6.2, 0.0
function TouchTarget() { // called once per frame in Update
var count : int = iPhoneInput.touchCount;
if(count == 1) {
var touch : iPhoneTouch = iPhoneInput.GetTouch(0);
if (touch.phase == iPhoneTouchPhase.Began) {
//var ray = Camera.main.ScreenPointToRay( Vector3( touch.position.x, touch.position.y ) );
var ray = Camera.main.ScreenPointToRay(touch.position);
var hit : RaycastHit;
if(Physics.Raycast(ray, hit)) {
targetLocation = hit.point;
targetLocation.z = 0.0; // ignore z
Debug.Log(targetLocation);
}
}
}
}
I've also tried getting the touch position in the following ways, but each of them has the same random inaccuracy.
// one way
targetLocation = hit.point;
targetLocation.z = 0.0; // ignore z
// another way
targetLocation = touch.position;
// yet another way
var targetLoc : Vector2 = hit.point;
targetLocation = Vector3 (targetLoc.x, targetLoc.y, 0);
Any suggestions on where I might be going wrong?