Input.GetTouch offset bug

Hi I’m having trouble with the following code. I’m trying to track the players finger and attach a cube to wherever the finger move. The following script is attached to the cube but I’m not getting the results I anticipated:

		if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
		 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
		transform.Translate (touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0.0f);
	}

It does track the players finger, however there’s an offset I wasn’t expecting. The further the players finger is to the right of the screen, the further the object is offset to the right of the player’s finger. The true is opposite when the players finger is at the left side of the screen. In fact it only maps the object to the players finger position exactly when their finger is in the middle of the screen!

Thanks for any assistance!

Where is your speed modifier set? That is obviously the changing factor in it's translate, and where most likely the offset is being created. You haven't shown us where the value is being calculated or set. As an fyi, Touch.position is also reported back as screen pixel coordinates, so you could technically have the transform.position just equal to the Touch.position pixel coordinates by transforming the world space to screen space coordinates with Camera.WorldToScreenPoint.

Great, I've made your suggested changes and it works a treat - thanks!

Glad it worked!

1 Answer

1

How I can know the speed with which you move your finger to go changing the variable speed?

that you changed in the code?