unity3d---移动、缩放、旋转

目录

1.示意图

2.触屏移动与缩放+键盘移动、旋转与缩放+鼠标移动旋转与缩放



1.示意图

unity3d---移动、缩放、旋转_第1张图片 unity3d---移动、缩放、旋转_第2张图片

unity3d---移动、缩放、旋转_第3张图片

2.触屏移动与缩放+键盘移动、旋转与缩放+鼠标移动旋转与缩放

using UnityEngine;

public class CameraMove : MonoBehaviour
{
    enum SlideVector
    {
        None,
        Up,
        Down,
        Left,
        Right
    };

    [SerializeField] private bool executeMultipleTimes = false; //是否支持多次执行
    [SerializeField] private float offsetTime = 0.1f; //判断的时间间隔 
    [SerializeField] private float slidingDistance = 50f; //滑动的最小距离

    private Vector2 touchFirst = Vector2.zero; //手指开始按下的位置
    private Vector2 touchSecond = Vector2.zero; //手指拖动的位置
    private Touch m_touchLeft;
    private Touch m_touchRight;
    private SlideVector currentVector = SlideVector.None; //当前滑动方向

    private float timer; //时间计数器

    [SerializeField]
    private Camera myCamera;
    private float eulerX = 0;
    private float eulerY = 0;

    private void OnEnable()
    {
        eulerX = myCamera.transform.localEulerAngles.x;
        eulerY = myCamera.transform.localEulerAngles.y;
    }
    void Update()
    {
#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
			HandleTouchInput();
#else
        HandleMouseInput();
#endif
    }

    //触屏
    void HandleTouchInput()
    {
        if (Input.touchCount == 1)
        {
            Touch touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began)
            {
                touchFirst = touch.position;
            }
            else if (touch.phase == TouchPhase.Moved)
            {
                touchSecond = touch.position;
                MoveCamera(true);
            }
        }
        else if (Input.touchCount == 2)
        {
            //判断左右触屏点
            if (Input.touches[0].position.x > Input.touches[1].position.x)
            {
                m_touchLeft = Input.touches[1];
                m_touchRight = Input.touches[0];
            }
            else
            {
                m_touchLeft = Input.touches[0];
                m_touchRight = Input.touches[1];
            }

            if (m_touchLeft.phase == TouchPhase.Moved && m_touchRight.phase == TouchPhase.Moved)
            {
                int num = IsEnlarge(m_touchLeft.position, m_touchRight.position);
                switch (num)
                {
                    case 1:
                        //Debug.LogError("放大");
                        ScaleCamera(true);
                        break;
                    case -1:
                        //Debug.LogError("缩小");
                        ScaleCamera(false);
                        break;
                }
            }
        }
    }

    private float speed => Time.deltaTime * 10;

    //鼠标
    void HandleMouseInput()
    {
        if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
        {
            touchFirst = Input.mousePosition;
        }
        //移动
        if (Input.GetMouseButton(0))
        {
            touchSecond = Input.mousePosition;
            timer += Time.deltaTime;
            if (timer > offsetTime)
            {
                touchSecond = Input.mousePosition;

                timer = 0;
                touchFirst = touchSecond;
            }
            MoveCamera(false);
        }//旋转
        else if (Input.GetMouseButton(1))
        {
            touchSecond = Input.mousePosition;
            timer += Time.deltaTime;
            if (timer > offsetTime)
            {
                touchSecond = Input.mousePosition;
                Vector2 slideDirection = touchFirst - touchSecond;
                float x = slideDirection.x;
                float y = slideDirection.y;

                if (y + slidingDistance < x && y > -x - slidingDistance)
                {
                    //Debug.Log("left");
                    currentVector = SlideVector.Left;
                }
                else if (y > x + slidingDistance && y < -x - slidingDistance)
                {
                    //Debug.Log("right");
                    currentVector = SlideVector.Right;
                }
                else if (y > x + slidingDistance && y - slidingDistance > -x)
                {
                    //Debug.Log("up");
                    currentVector = SlideVector.Up;
                }
                else if (y + slidingDistance < x && y < -x - slidingDistance)
                {
                    //Debug.Log("Down");
                    currentVector = SlideVector.Down;
                }

                timer = 0;
                touchFirst = touchSecond;
            }
            RotateCamera(currentVector);
        }
        else
        {
            //滚轮缩放 与 键盘移动、旋转
            if (Input.mouseScrollDelta.y > 0)
            {
                ScaleCamera(true);
            }
            else if (Input.mouseScrollDelta.y < 0)
            {
                ScaleCamera(false);
            }
            //键盘ADWS移动与上下左右键旋转
            if (Input.GetKey(KeyCode.A))
            {
                minPos_x = GetMoveX(false);
                maxPos_x = GetMoveX(true);
                myCamera.transform.position = new Vector3(Mathf.Clamp(myCamera.transform.position.x - speed, minPos_x, maxPos_x), Mathf.Clamp(myCamera.transform.position.y, minPos_y, maxPos_y), -100);
            }
            else if (Input.GetKey(KeyCode.D))
            {
                minPos_x = GetMoveX(false);
                maxPos_x = GetMoveX(true);
                myCamera.transform.position = new Vector3(Mathf.Clamp(myCamera.transform.position.x + speed, minPos_x, maxPos_x), Mathf.Clamp(myCamera.transform.position.y, minPos_y, maxPos_y), -100);
            }
            else if (Input.GetKey(KeyCode.W))
            {
                myCamera.transform.position = new Vector3(Mathf.Clamp(myCamera.transform.position.x, minPos_x, maxPos_x), Mathf.Clamp(myCamera.transform.position.y + speed, minPos_y, maxPos_y), -100);
            }
            else if (Input.GetKey(KeyCode.S))
            {
                myCamera.transform.position = new Vector3(Mathf.Clamp(myCamera.transform.position.x, minPos_x, maxPos_x), Mathf.Clamp(myCamera.transform.position.y - speed, minPos_y, maxPos_y), -100);
            }
            else if (Input.GetKey(KeyCode.UpArrow))
            {
                RotateCamera(SlideVector.Up);
            }
            else if (Input.GetKey(KeyCode.DownArrow))
            {
                RotateCamera(SlideVector.Down);
            }
            else if (Input.GetKey(KeyCode.LeftArrow))
            {
                RotateCamera(SlideVector.Left);
            }
            else if (Input.GetKey(KeyCode.RightArrow))
            {
                RotateCamera(SlideVector.Right);
            }
        }
    }

    private Vector2 offset = Vector2.zero;
    /// 
    /// 移动相机
    /// 
    /// 
    private void MoveCamera(bool isPhone)
    {
        if (isPhone)
        {
            offset = ((myCamera.ScreenToWorldPoint(new Vector3(touchSecond.x, touchSecond.y, myCamera.nearClipPlane)) - myCamera.ScreenToWorldPoint(new Vector3(touchFirst.x, touchFirst.y, myCamera.nearClipPlane))));
        }
        else
        {
            offset = ((myCamera.ScreenToWorldPoint(new Vector3(touchSecond.x, touchSecond.y, myCamera.nearClipPlane)) - myCamera.ScreenToWorldPoint(new Vector3(touchFirst.x, touchFirst.y, myCamera.nearClipPlane)))) / 20f;
        }
        //Debug.LogError("offset:" + offset.ToString());
        Vector2 newPos = new Vector3(myCamera.transform.position.x + offset.x, myCamera.transform.position.y + offset.y);
        newPos.x = Mathf.Clamp(newPos.x, GetMoveX(false), GetMoveX(true));
        newPos.y = Mathf.Clamp(newPos.y, minPos_y, maxPos_y);
        myCamera.transform.position = new Vector3(newPos.x, newPos.y, -100);
    }

    /// 
    /// 鼠标旋转相机
    /// 
    private void RotateCamera(SlideVector cur)
    {
        switch (cur)
        {
            case SlideVector.Left:
                minRot_y = GetRotationY(false);
                maxRot_y = GetRotationY(true);
                eulerY = Mathf.Clamp(eulerY - speed, minRot_y, maxRot_y);
                myCamera.transform.localRotation = Quaternion.Euler(eulerX, eulerY, 0);
                break;
            case SlideVector.Right:
                minRot_y = GetRotationY(false);
                maxRot_y = GetRotationY(true);
                eulerY = Mathf.Clamp(eulerY + speed, minRot_y, maxRot_y);
                myCamera.transform.localRotation = Quaternion.Euler(eulerX, eulerY, 0);
                break;
            case SlideVector.Up:
                minRot_x = GetRotationX(false);
                maxRot_x = GetRotationX(true);
                eulerX = Mathf.Clamp(eulerX + speed, minRot_x, maxRot_x);
                myCamera.transform.localRotation = Quaternion.Euler(eulerX, eulerY, 0);
                break;
            case SlideVector.Down:
                minRot_x = GetRotationX(false);
                maxRot_x = GetRotationX(true);
                eulerX = Mathf.Clamp(eulerX - speed, minRot_x, maxRot_x);
                myCamera.transform.localRotation = Quaternion.Euler(eulerX, eulerY, 0);
                break;
        }
    }
    /// 
    /// 缩放相机  true放大
    /// 
    public void ScaleCamera(bool isBig)
    {
        if (isBig)
        {
            if (myCamera.orthographicSize > minSize && myCamera.orthographicSize <= maxSize)
            {
                myCamera.orthographicSize -= 1;
            }
        }
        else
        {
            if (myCamera.orthographicSize >= minSize && myCamera.orthographicSize < maxSize)
            {
                myCamera.orthographicSize += 1;
            }
        }
    }

    //用于判断是否放大
    float m_leng0 = 0;
    private int IsEnlarge(Vector2 P1, Vector2 P2)
    {
        float leng1 = Vector2.Distance(P1, P2);
        if (m_leng0 == 0)
        {
            m_leng0 = leng1;
        }
        if (m_leng0 < leng1)
        {
            //放大手势
            m_leng0 = leng1;
            return 1;
        }
        else if (m_leng0 > leng1)
        {
            //缩小手势
            m_leng0 = leng1;
            return -1;
        }
        else
        {
            m_leng0 = leng1;
            return 0;
        }
    }

    #region------------------摄像机左右移动、左右上下旋转限制范围------------------------------------------
    //移动范围
    private float minPos_x = -60;
    private float maxPos_x = 60;
    private float minPos_y = -25;
    private float maxPos_y = 50;
    //旋转范围
    private float minRot_x = -30;
    private float maxRot_x = 15;
    private float minRot_y = -30;
    private float maxRot_y = 30;
    //size 大小
    private float minSize = 30;
    private float midSize = 46;
    private float maxSize = 144;

    /// 
    /// 根据rotation的Y 获取摄像机移动的x范围
    /// 
    private float GetMoveX(bool isMax)
    {
        float num = 0;
        if (isMax)
        {
            if (myCamera.transform.localEulerAngles.y > 0 && myCamera.transform.localEulerAngles.y < 180)
            {
                num = 60 - myCamera.transform.localEulerAngles.y * 2;
            }
            else if (myCamera.transform.localEulerAngles.y > 180)
            {
                num = 60 - (myCamera.transform.localEulerAngles.y - 360) * 1.5f;
            }
            else
            {
                num = 60;
            }
        }
        else
        {
            if (myCamera.transform.localEulerAngles.y > 0 && myCamera.transform.localEulerAngles.y <= 10)
            {
                num = -60 - myCamera.transform.localEulerAngles.y;
            }
            else if (myCamera.transform.localEulerAngles.y > 10 && myCamera.transform.localEulerAngles.y < 180)
            {
                num = -70 - (myCamera.transform.localEulerAngles.y - 10) * 2;
            }
            else if (myCamera.transform.localEulerAngles.y > 180)
            {
                num = -60 - (myCamera.transform.localEulerAngles.y - 360) * 2;
            }
            else
            {
                num = -60;
            }
        }
        return num;
    }
    /// 
    /// 获取x轴旋转范围
    /// 
    private float GetRotationX(bool isMax)
    {
        float num = 0;
        if (isMax)
        {
            if (myCamera.transform.position.y > 0 && myCamera.transform.position.y <= 10)
            {
                num = 15 + myCamera.transform.position.y;
            }
            else if (myCamera.transform.position.y > 10)
            {
                num = 25 + (myCamera.transform.position.y / 2);
            }
            else if (myCamera.transform.position.y < 0)
            {
                num = 15 - (myCamera.transform.position.y / 2);
            }
            else
            {
                num = 15;
            }
        }
        else
        {
            if (myCamera.transform.position.y == 0)
            {
                num = -30;
            }
            else
            {
                num = -30 + (myCamera.transform.position.y / 2);
            }
        }

        return num;
    }
    /// 
    /// 获取y轴旋转范围
    /// 
    private float GetRotationY(bool isMax)
    {
        float num = 0;
        if (isMax)
        {
            if (myCamera.transform.position.x > 0)
            {
                num = 30 - (myCamera.transform.position.x / 2);
            }
            else if (myCamera.transform.position.x < 0)
            {
                num = 30 - (myCamera.transform.position.x / 2);
            }
            else
            {
                num = 30;
            }
        }
        else
        {
            if (myCamera.transform.position.x > 0)
            {
                num = -30 - (myCamera.transform.position.x / 2);
            }
            else if (myCamera.transform.position.x < 0)
            {
                num = -30 - (myCamera.transform.position.x / 2);
            }
            else
            {
                num = -30;
            }
        }

        return num;
    }
    #endregion
}

你可能感兴趣的:(Unity,unity3d,unity,鼠标事件,unty,按键,unity,触屏,unity旋转)