前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >u3d拖动摄像机视角与双指缩放

u3d拖动摄像机视角与双指缩放

作者头像
立羽
发布2023-08-24 13:41:11
发布2023-08-24 13:41:11
35000
代码可运行
举报
文章被收录于专栏:Unity3d程序开发Unity3d程序开发
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
复制
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;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-05-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档