x


How to get accurate touch.position?

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?

more ▼

asked Dec 18 '09 at 04:52 PM

rocket5tim gravatar image

rocket5tim
859 12 17 33

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Turns out that the problem is not with the code, but rather with my scene. Since I'm making a 2D side-scroller, I didn't have any collision on the background (nothing ever moves in Z so why would I need collision there?). After adding a collision box across my background, I am now getting accurate touch.position and hit.point.

more ▼

answered Dec 30 '09 at 05:52 PM

rocket5tim gravatar image

rocket5tim
859 12 17 33

(comments are locked)
10|3000 characters needed characters left

I'm not very fermiliar with iPhone specifics, but since your question hasn't had an answer yet, I'll shoot anyway: It looks like you're not correctly dealing with the situation where there would be more than one touches. Instead of dealing with all of them, you currently deal with none of them. You only do something if there is exactly one touch.

more ▼

answered Dec 20 '09 at 10:40 PM

Lucas Meijer 1 gravatar image

Lucas Meijer 1 ♦♦
8k 19 43 85

In this example yes I'm only allowing 1 touch at a time. But that shouldn't affect the touch position. I suspect the problem might lie in the difference between touch.position and hit.point (both of those return different values). But I'm not sure how to get the "correct" point on screen.

Dec 23 '09 at 10:32 PM rocket5tim
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5099
x2001

asked: Dec 18 '09 at 04:52 PM

Seen: 7592 times

Last Updated: Dec 18 '09 at 04:52 PM