Registering Accurate touch position

Hello,

I am looking to improve my code for registering touch positions on the screen,. Right now i do it like this…

ObjectX_Position = Cam.camera.WorldToViewportPoint(ObjectX.transform.position);

if (Input.touchCount > 0 Input.GetTouch(0).phase == TouchPhase.Began)
{

t = Input.touches[0];
TouchStart = t.position;
TouchPosition = Cam.camera.ScreenToViewportPoint(TouchStart);

}

And then i compare these 2 variables like this… if (Vector2.Distance(TouchPosition,ObjectX_Position) < 0.1) { /* Rotate Object */ } …

But for some reason this is not accurate enough, it can happen for instance that if i touch the screen approximately 0.5 cm, outside this zone the touch will still be registered. Setting the margin to anything lower than 0.1 will not do, because then it won’t work at all… If some of you might happen to know a solution to this, i would be very pleased with your advice…

Thanks in advance,
Yours Truly.

You are working in viewport sizes, so I don’t know how that relates to your 0.5 cm accuracy.

Overall what you are doing seems a bit complicated. Rather than convert both positions to ViewportPoint, I would just convert the touch position to a WorldPoint with camera.ScreenToWorldPoint then use a Vector3 distance (or sqrMagnitude which I believe is faster than distance).

Yeah, if I remember correctly. I had some problems with all those conversions because I’ve tried about all of them… Until I did something that would work, so I got stuck with that. But I’ll have to try, what you did mention it is absolutely possible that’ll work. And that would be very nice, to say the least.

Thanks