[Unity]拖动摄像机视角与双指缩放

using UnityEngine;
using System.Collections;

public class CamMove : MonoBehaviour
{
    private float m_xSpeed = 250.0f;
    private float m_ySpeed = 120.0f;

    private float m_yMinLimit = -45;
    private float m_yMaxLimit = 45;

    private float m_xAngles = 0.0f;
    private float m_yAngles = 0.0f;

    Vector2 m_oldPos0;
    Vector2 m_oldPos1;

    public float m_scale = 1.0f;
    float m_scaleStep = 0.1f;
    float m_scaleMax = 2.0f;

    public bool m_enable = true;
    void Start()
    {
        Vector3 angles = transform.eulerAngles;
        m_xAngles = angles.y;
        m_yAngles = angles.x;
    }

    void Update()
    {
        if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            if (m_enable)
            {
                if (Input.GetMouseButton(0))
                {
                    m_xAngles -= Input.GetAxis("Mouse X") * m_xSpeed * 0.02f;
                    m_yAngles += Input.GetAxis("Mouse Y") * m_ySpeed * 0.02f;
                }
                else
                {
                    if (Input.GetAxis("Mouse ScrollWheel") < 0)
                    {
                        FullRenderScale(false, 0.1f);
                    }
                    //Zoom in  
                    if (Input.GetAxis("Mouse ScrollWheel") > 0)
                    {
                        FullRenderScale(true, 0.1f);
                    }
                }
            }
        }
        else
        {
            if (m_enable)
            {
                if (Input.touchCount == 1)
                {
                    if (Input.GetTouch(0).phase == TouchPhase.Moved)
                    {
                        m_xAngles -= Input.GetAxis("Mouse X") * m_xSpeed * 0.02f;
                        m_yAngles += Input.GetAxis("Mouse Y") * m_ySpeed * 0.02f;

                    }
                }

                else if (Input.touchCount > 1)
                {
                    if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
                    {

                        var curPos0 = Input.GetTouch(0).position;
                        var curPos1 = Input.GetTouch(1).position;

                        if (IsEnlarge(m_oldPos0, m_oldPos1, curPos0, curPos1))
                        {
                            FullRenderScale(true, 0.01f);
                        }
                        else
                        {

                            FullRenderScale(false, 0.01f);
                        }
                        m_oldPos0 = curPos0;
                        m_oldPos1 = curPos1;
                    }
                }
            }
        }
    }

    bool  IsEnlarge(Vector2 old0,Vector2 old1,Vector2 cur0,Vector2 cur1) 
    {
        var leng1 = Mathf.Sqrt((old0.x - old1.x) * (old0.x - old1.x) + (old0.y - old1.y) * (old0.y - old1.y));
        var leng2 = Mathf.Sqrt((cur0.x - cur1.x) * (cur0.x - cur1.x) + (cur0.y - cur1.y) * (cur0.y - cur1.y));
        if(leng1<leng2)
        {
             return true;
        }
        else
        {
            return false;
        }
    }

    void LateUpdate()
    {
        if (m_enable)
        {
            m_yAngles = ClampAngle(m_yAngles, m_yMinLimit, m_yMaxLimit);
            Quaternion rotation = Quaternion.Euler(m_yAngles, m_xAngles, 0);
            transform.rotation = rotation;
        }
    }

    static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }

    void FullRenderScale(bool isBig,float scale)
    {
        if (isBig)
        {
            if (m_scale <= m_scaleMax)
            {
                m_scale += scale;
            }
        }
        else
        {
            if (m_scale > 1.0f)
            {
                m_scale -= scale;
            }
        }
        MainMgr.Instance.m_fullRender.transform.localScale = new Vector3(m_scale, m_scale, m_scale);
    }

    private void OnEnable()
    {
        m_enable = true;
    }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万兴丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值