0
votes

In my scene I have a camera as a child of an empty game object. In my script I have the ability to rotate around the parent object, translate the parent object, and zoom my camera by moving it on the Z axis.

However, what I'm wanting is to have my cameras Y position remain the same when the parent is moved. In the sample I have posted below, I have worked out how to achieve this. But I am no longer able to zoom in and out anymore.

I've been looking at this for a while now, and I've a feeling its something daft I'm not doing. But could someone take a look over my zooming and translating methods to see what I'm doing wrong / missing?

// Pinch fingers to zoom camera
// Will move camera on the Z access
void CameraZoom ()
{

    // if fingers moved certain distance apart, zoom
    float dot = Vector2.Dot (Input.GetTouch (0).deltaPosition.normalized, Input.GetTouch (1).deltaPosition.normalized);

    if (dot < -fingerDistance) 
    {

        // Store both touches.
        Touch touchZero = Input.GetTouch (0);
        Touch touchOne = Input.GetTouch (1);

        Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
        Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

        float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
        float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
        float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

        if (useFieldOfView == true) 
        {
            cam.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
            // Clamp the field of view to make sure it's between 0 and 180.
            cam.fieldOfView = Mathf.Clamp (cam.fieldOfView, 0.1f, 179.9f);
        }
        else if (useFieldOfView == false) 
        {
            transform.position = new Vector3 (transform.position.x , transform.position.y , transform.position.z );
            transform.Translate (0, 0, deltaMagnitudeDiff * perspectiveZoomSpeed);
        }
    }
}

void MoveCamera ()
{

    Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
    // this is the line keeping my camera stationary on the Y axis
    transform.position = new Vector3 (transform.position.x , cameraCurrentYHeight , transform.position.z );
    transform.parent.Translate (-touchDeltaPosition.x * speed, 0, -touchDeltaPosition.y * speed);


    if(transform.parent.position.y <= yMovementLowestValue)
    {
        transform.parent.position = new Vector3(transform.parent.position.x, yMovementLowestValue, transform.parent.position.z);
    }
}

My original idea was to take a "snap shot" of the cameras current height, when the parent is moving. Then, as the parent moves, simply keep the camera at this constant height. I do this by checking that two touches have been registered, and that they are stationary, then when the move, the zoom and movement methods are called. All of this, can be found in my update method as follows:

    if (Input.touchCount == 2 && Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (1).phase == TouchPhase.Moved) 
    {
        CameraZoom ();
        MoveCamera ();  
        takeSnapShot = false;

    }


    if(Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Stationary && Input.GetTouch(1).phase == TouchPhase.Stationary)
    {

        takeSnapShot = true;
    }

    if(takeSnapShot == true)
    {
        cameraCurrentYHeight = cam.transform.position.y;
    }
2

2 Answers

1
votes

Dot product of two normalized vectors will always give you 1

float dot = Vector2.Dot (Input.GetTouch (0).deltaPosition.normalized, Input.GetTouch (1).deltaPosition.normalized);

I think what you meant to do is:

float dot = (Input.GetTouch (0).deltaPosition - Input.GetTouch (1).deltaPosition).sqrMagnitude;

Also, since you are using the square magnitude, i'm sure because you are concerned with performance, you want to compare the two squares together in the if statement as well, like so:

if (dot < fingerDistance * fingerDistance) 
0
votes

Why not move the camera out of the emptyGameObject and just update the x and z position of the camera with the emptyGameObject x and z.

public GameObject control;    
..

control.transform.Translate (-touchDeltaPosition.x * speed, 0, -touchDeltaPosition.y * speed);
transform.position = new Vector3(control.transform.position.x, transform.position.y, control.transform.position.z);