缩放和翻译统一相机

本文关键字:相机 翻译 缩放 | 更新日期: 2023-09-27 18:16:23

在我的场景中,我有一个相机作为一个空游戏对象的子对象。在我的脚本中,我能够围绕父对象旋转,平移父对象,并通过在Z轴上移动来缩放摄像机。

然而,我想要的是有我的相机Y位置保持不变,当父移动。在我下面发布的示例中,我已经研究出如何实现这一点。但是我不能再放大和缩小了。

我已经看了一段时间了,我有一种感觉,我没有做一些愚蠢的事情。但是有人可以看看我的缩放和翻译方法,看看我做错了什么/失踪?

// 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);
    }
}

我最初的想法是在父母移动时拍摄相机当前高度的"快照"。然后,当父母移动时,只要保持摄影机在这个恒定的高度。我通过检查两个触摸被注册,它们是静止的来做这个,然后当移动,缩放和移动功能被调用。所有这些,都可以在我的更新方法中找到,如下所示:

    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;
    }

缩放和翻译统一相机

两个归一化向量的点积总是等于1

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

我想你的意思是:

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

此外,由于您正在使用平方幅度,我确信因为您关心性能,您也想在if语句中比较两个平方,如下所示:

if (dot < fingerDistance * fingerDistance) 

为什么不将摄像机移出emptyGameObject,而只是使用emptyGameObject x和z更新摄像机的x和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);