Move object in x-direction same distance as finger movement

Hi Guys,

I thought what I was trying to was simple, but I can’t get it right. I have an object that I want to move, and the main camera follows the object via a simple follow script. When a player slides their finger on the screen in the x direction, I want the object to move exactly that much in the x direction as well (in relation to the level, as the object is always in the center of the screen). No matter what I do, it seems my finger is moving around twice as far as the object. The complexity that I’m not understand comes from the fact that the camera is moving with the object.

This is the current code I have, where I try to use relative distances from their initial locations to move the object. Not working though…

void FixedUpdate ()
    {
        if (Input.touchCount > 0) {
       
            Touch touch = Input.GetTouch(0);

            if (touch.phase == TouchPhase.Began)
            {
                objectStartPosition = objectTransform.position;
                touchStartPosition = touch.position;
            }
            else
            {
                objectLocation.x = objectStartPosition.x + ( touch.position.x - touchStartPosition.x) / 100;
                PlaceObject(objectLocation);
            }
   }

If anyone could help, I would be immensely thankful!

Thanks guys.

Why would you divide the distance by 100? You need to use something along the lines of “Camera.main.ScreenToWorldPoint()” to convert from the touch’s position to an actual world position.

Sorry, I should have explained that. I initially had “Camera.main.ScreenToWorldPoint()” in my code. As I was playing around I swapped some stuff. Dividing delta screen position (which is is pixels) by 100 basically converts the pixels to world space units. This only works because my textures are set to 100 pixels per unit, I’m using a 1080P screen, and I have my camera set to pixel perfect settings (a.k.a. size of 5.4).