繁体   English   中英

PinchToZoom 功能不起作用 C# Unity

[英]PinchToZoom functionality does not work C# Unity

在从鸟瞰视图显示对象的应用程序中,它可以识别触摸以及手指移动,但缩放功能仍然关闭。

所以最后什么都没有发生,除了手指和动作被识别出来。

我不明白为什么会这样。

using UnityEngine;




public class ViewerCameraController : MonoBehaviour
{
    private Camera cam;
    private RaycastHit raycastHit;
    public Collider col;
    public List<GameObject> objects;


    Vector3 touchStart;
    public float zoomOutMin = 1;
    public float zoomOutMax = 8;



    private void Awake()
    {

    }

    // Start is called before the first frame update
    void Start()
    {
        cam = GetComponent<Camera>();

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
        if (Input.touchCount == 2)
        {
            Touch touchZero = Input.GetTouch(0);
            Touch touchOne = Input.GetTouch(1);

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

            float prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            float currentMagnitude = (touchZero.position - touchOne.position).magnitude;

            float difference = currentMagnitude - prevMagnitude;

            zoom(difference * 2.01f);
        }
        else if (Input.GetMouseButton(0))
        {
            Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Camera.main.transform.position += direction;
        }
        zoom(Input.GetAxis("Mouse ScrollWheel"));
    }

    void zoom(float increment)
    {
        Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize - increment, zoomOutMin, zoomOutMax);
    }
}```

这是因为您没有增加相机变焦的“增量”值。 您还可以根据需要将增量与速度相乘。 我还在 Update() 中添加了必要的 zoomMin 和 zoomMax function。 我已经更新了你的脚本。

我不明白您为什么要将差异与更高的值相乘,所以在我的脚本中我删除了它 - zoom (difference, zoomSpeed);

    private Camera cam;
    private RaycastHit raycastHit;
    public Collider col;
    public List<GameObject> objects;

    Vector3 touchStart;
    public float zoomOutMin = 1;
    public float zoomOutMax = 170;

    float zoomSpeed = 2.0f;

    private void Awake () {

    }

    // Start is called before the first frame update
    void Start () {
        cam = GetComponent<Camera> ();

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown (0)) {
            touchStart = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        }
        if (Input.touchCount == 2) {
            Touch touchZero = Input.GetTouch (0);
            Touch touchOne = Input.GetTouch (1);

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

            float prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            float currentMagnitude = (touchZero.position - touchOne.position).magnitude;

            float difference = currentMagnitude - prevMagnitude;

            zoom (difference, zoomSpeed);
        } else if (Input.GetMouseButton (0)) {
            Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint (Input.mousePosition);
            Camera.main.transform.position += direction;
        }
        zoom (Input.GetAxis ("Mouse ScrollWheel"), zoomSpeed);

        if (cam.fieldOfView < zoomOutMin) {
            cam.fieldOfView = zoomOutMin;
        } else
        if (cam.fieldOfView > zoomOutMax) {
            cam.fieldOfView = zoomOutMax;
        }
    }

    void zoom (float increment, float speed) {
        cam.fieldOfView += increment * speed;
        cam.fieldOfView = Mathf.Clamp (cam.fieldOfView, zoomOutMin, zoomOutMax);
    }

问题是 Unity 本身的一个小设置,使用的相机没有设置为 Othographic....

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM