放大统一移动

Zooming in unity mobile

我正在尝试使用缩放限制在我的游戏中进行放大和缩小。我正在使用此代码进行滚动和捏合,但我没有弄清楚如何限制缩放距离(minzoomlimit 和 maxzoomlimit)。我的意思是,当玩家想要放大​​时,他可以放大一定距离,当他想要缩小时,他可以放大一定距离。

这是我用来滚动和捏合的代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScrollAndPinch : MonoBehaviour
{
#if UNITY_IOS || UNITY_ANDROID
    public Camera Camera;
    public bool Rotate;
    protected Plane Plane;
    public float perspectiveZoomSpeed = 0.5f;        // The rate of change of the field of view in perspective mode.
    public float orthoZoomSpeed = 0.5f;





    public Transform Map;

    float distance;

    float MaxDistance = 37063.57f; /*= new Vector3(-3724.8f, 34576.9f, -4562.5f);*/
    float MinDistance = 123302.2f; /*= new Vector3(-18149.0f, 180315.6f, -73023.5f);*/

    private void Awake()
    {
        if (Camera == null)
            Camera = Camera.main;
        Input.multiTouchEnabled = true;

    }

    private void Update()
    {

        //Update Plane
        if (Input.touchCount >= 1)
            Plane.SetNormalAndPosition(transform.up, transform.position);

        var Delta1 = Vector3.zero;
        var Delta2 = Vector3.zero;

        //Scroll
        if (Input.touchCount >= 1)
        {
            Delta1 = PlanePositionDelta(Input.GetTouch(0));
            if (Input.GetTouch(0).phase == TouchPhase.Moved)
                Camera.transform.Translate(Delta1, Space.World);
        }


        //Pinch
        if (Input.touchCount >= 2)
        {
            var pos1 = PlanePosition(Input.GetTouch(0).position);
            var pos2 = PlanePosition(Input.GetTouch(1).position);
            var pos1b = PlanePosition(Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition);
            var pos2b = PlanePosition(Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition);

            //calc zoom
            var zoom = Vector3.Distance(pos1, pos2) /
                       Vector3.Distance(pos1b, pos2b);
            //Debug.Log(zoom);
            //edge case
            if (zoom == 0 || zoom > 10)
                return;

            //Move cam amount the mid ray
            // Camera.fieldOfView = Mathf.Clamp(Camera.fieldOfView, MinDistance, MaxDistance);
            distance = Vector3.Distance(Camera.transform.position, Map.position);
            Camera.transform.position = Vector3.LerpUnclamped(pos1, Camera.transform.position, 1/zoom);




            if (Rotate && pos2b != pos2)

            Camera.transform.RotateAround(pos1, Plane.normal, Vector3.SignedAngle(pos2 - pos1, pos2b - pos1b, Plane.normal));







        }

    }

    protected Vector3 PlanePositionDelta(Touch touch)
    {
        //not moved
        if (touch.phase != TouchPhase.Moved)
            return Vector3.zero;

        //delta
        var rayBefore = Camera.ScreenPointToRay(touch.position - touch.deltaPosition);
        var rayNow = Camera.ScreenPointToRay(touch.position);
        if (Plane.Raycast(rayBefore, out var enterBefore) && Plane.Raycast(rayNow, out var enterNow))
            return rayBefore.GetPoint(enterBefore) - rayNow.GetPoint(enterNow);

        //not on plane
        return Vector3.zero;
    }

    protected Vector3 PlanePosition(Vector2 screenPos)
    {
        //position
        var rayNow = Camera.ScreenPointToRay(screenPos);
        if (Plane.Raycast(rayNow, out var enterNow))
            return rayNow.GetPoint(enterNow);

        return Vector3.zero;
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawLine(transform.position, transform.position + transform.up);
    }
#endif
}

任何帮助,谢谢 :D 。

非常简单,您只需在更新函数中添加这段代码即可;

void Update()
{
         if(cam.fieldOfView < minimunDistance)
         {
             cam.fieldOfView = minimunDistance;
         }
         else
         if(cam.fieldOfView > maxDistance)
         {
             cam.fieldOfView = maxDistance;
         }
}

或者,我有一个捏合和缩放脚本,应该可以在触摸输入设备和鼠标滚轮上使用;


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PinchAndZoom : MonoBehaviour
{
    float MouseZoomSpeed = 15.0f;
    float TouchZoomSpeed = 0.1f;
    float ZoomMinBound = 0.1f;
    float ZoomMaxBound = 179.9f;
    Camera cam;

    // Use this for initialization
    void Start()
    {
        cam = GetComponent<Camera>();
    }

    void Update()
    {
        if (Input.touchSupported)
        {
            // Pinch to zoom
            if (Input.touchCount == 2)
            {

                // get current touch positions
                Touch tZero = Input.GetTouch(0);
                Touch tOne = Input.GetTouch(1);
                // get touch position from the previous frame
                Vector2 tZeroPrevious = tZero.position - tZero.deltaPosition;
                Vector2 tOnePrevious = tOne.position - tOne.deltaPosition;

                float oldTouchDistance = Vector2.Distance (tZeroPrevious, tOnePrevious);
                float currentTouchDistance = Vector2.Distance (tZero.position, tOne.position);

                // get offset value
                float deltaDistance = oldTouchDistance - currentTouchDistance;
                Zoom (deltaDistance, TouchZoomSpeed);
            }
        }
        else
        {

            float scroll = Input.GetAxis("Mouse ScrollWheel");
            Zoom(scroll, MouseZoomSpeed);
        }



         if(cam.fieldOfView < ZoomMinBound) 
         {
             cam.fieldOfView = 0.1f;
         }
         else
         if(cam.fieldOfView > ZoomMaxBound ) 
         {
             cam.fieldOfView = 179.9f;
         }
    }

    void Zoom(float deltaMagnitudeDiff, float speed)
    {

        cam.fieldOfView += deltaMagnitudeDiff * speed;
        // set min and max value of Clamp function upon your requirement
        cam.fieldOfView = Mathf.Clamp(cam.fieldOfView, ZoomMinBound, ZoomMaxBound);
    }
}