Move camera center point distance based on touch movement.

Hell. I’m having some trouble getting touch drag-to-pan function from working correctly depending on the rotation of the camera’s parent object.

I based the method on this video, which seems to be the closest thing to what I want. I’ve gotten other methods to work, it was hard to keep the objects movement proportional to the amount my finger moved across the screen.

I have the following set up:

I have a camera that is childed to an empty object (at the center of the blue circle) because I want the camera to orbit around this empty object. I then have another empty, the green dot, as a reference point, it’s facing the camera always because it’s childed to the camera.

I have the following script to pan the camera based on the midpoint of where my 2 fingers touched and dragged:

private void Panning() //pans the cart tilt object up to the pan limits.
    {
        if (Input.touchCount != 2)
            return;

        if ((Input.GetTouch(0).phase != TouchPhase.Moved && Input.GetTouch(1).phase != TouchPhase.Moved))
            return;

        if (EventSystem.current.IsPointerOverGameObject(Input.touchCount - 1))
            return;

        TouchTimer();

        Touch touch1 = Input.GetTouch(0);
        Touch touch2 = Input.GetTouch(1);

        if (Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            Vector3 currentPos = GetWorldPosition((touch1.position + touch2.position)/2);
            Vector3 previousPos = GetWorldPosition(((touch1.position - touch1.deltaPosition) + (touch2.position - touch2.deltaPosition)) / 2);
            Vector3 difference = currentPos - previousPos;
            Vector3 camPos = nonARCamera.transform.localPosition;
            camPos += new Vector3(-difference.x * panSensitivity, -difference.y * panSensitivity, 0);
            camPos.x = Mathf.Clamp(camPos.x, -panLimit[0], panLimit[1]);
            camPos.y = Mathf.Clamp(camPos.y, -panLimit[1], panLimit[1]);
            nonARCamera.transform.localPosition = camPos;
        }
    }

    private Vector3 GetWorldPosition(Vector2 _touchPos)
    {
        Ray rayPos = nonARCamera.ScreenPointToRay(_touchPos);     
        Plane plane = new Plane(cameraPlaneRef.forward, cameraPlaneRef.position);
        float distance;
        plane.Raycast(rayPos, out distance);
        return rayPos.GetPoint(distance);
    }

Basically, every time there is a 2-finger drag, a Plane is created where the green dot reference object is in front of the camera. I then calculate the position of the drag between the current and last frames, and then move the camera’s local position relative to its parent in the opposite direction (making the illusion that the object the camera is look at is moving the same way as the drag).

This works, but only in one direction. If the camera-parent is rotated to the other side of the object, the movement is backwards. If it’s on either side (+/- 90 degrees), nothing happens. I’m not sure what I’m doing wrong here. I know it has to do with the world positions of the current and previous being flipped when you’re behind the target, but I’m not sure how I could rotate the result based on how the camera pivot is rotated.

Any help would be appreciated. :slight_smile:

Not sure where you’re going wrong but there are drag-camera and drag-object demo scenes in my Proximity Buttons package. They’re called DemoDragCamera and DemoWorldDragging

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

Here’s what I THINK is wrong. When the camera plane not rotated, the previous and current positions come out a certain way. Say if I move the touch point from left to right, the current touch position is greater in the X-value than the previous touch position, so it moves normally. However, when the camera plane is rotated to the opposite side, the difference is now reversed, so it moves the opposite way. When they’re on either side, there is no difference in X-value, because they’re on the same x-coordinate.

I think I need to do some vector coordinate shenanigan’s with Sines and Cosine to rotate the points about an axis before doing the calculation. Not sure how I’ll go about doing that yet. Vector math hurts my head as it is.

Honestly it might be easier to just rotate and pan the object instead of the camera.

That’s a hypothesis… great! Now test it! Instrument the code with Debug.Log() and find out for sure!

Problem fixed. Switch from rotating a camera to just moving the target instead, so I don’t have to deal with rotating coordinate systems. Same methodology.