zoukankan      html  css  js  c++  java
  • unity windowEditor平台下鼠标左键控制摄像机的视角

    工作的原因,今天就只写了unity下的鼠标左键控制摄像机的视角左右上下调节;明天,补齐。[有诸多参考,着实是需要多多加油的]

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class CameraMove : MonoBehaviour
    {
        public Vector3 target;
        private Vector3 offset;
        private bool IsControlMove = true;
        private float _xAngles = 0.0f;
        private float _yAngles = 0.0f;
        private float m_xAngles = 0.0f;
        private float m_yAngles = 0.0f;
        private float m_xSpeed = 100f;
        private float m_ySpeed = 100f;
        //Limit
        private float m_xMinValue = -15f;
        private float m_xMaxValue = 15;
        private float m_yMinValue = -15;
        private float m_yMaxValue = 15;
        void Awake()
        {
            //Init
            Vector3 myCameraAngles = transform.eulerAngles;
            _xAngles = myCameraAngles.y;
            _yAngles = myCameraAngles.x;
            m_xAngles = myCameraAngles.y;
            m_yAngles = myCameraAngles.x;
            offset = transform.position - target;
        }    
        void LateUpdate()
        {
            if (IsControlMove)
            {           
                if (Application.platform == RuntimePlatform.WindowsEditor)
                {            
                    if (Input.GetMouseButton(0)/*&&!EventSystem.current.IsPointerOverGameObject()*/)
                    { 
                        m_xAngles -= Input.GetAxis("Mouse X") * m_xSpeed * 0.02f;
                        m_xAngles = Mathf.Clamp(m_xAngles, m_xMinValue, m_xMaxValue);
                        m_yAngles += Input.GetAxis("Mouse Y") * m_ySpeed * 0.02f;
                        m_yAngles = Mathf.Clamp(m_yAngles, m_yMinValue, m_yMaxValue);
                        var rotation = Quaternion.Euler(m_yAngles, m_xAngles, 0);
                        Vector3 position = rotation * offset + target;
                        transform.rotation = rotation;
                        transform.position = position;
                    }               
                } 
            }
        }  
    }
    

      

    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    在.NET中读取嵌入和使用资源文件的方法
    T-SQL with关键字 with as 递归循环表
    IIS 部署WCF时遇到这么个错:
    WCF引用 代码
    C#中Windows通用的回车转Tab方法
    HTTP 错误 500.21
    如果你想开发一个应用(1-14)
    如果你想开发一个应用(1-13)
    如果你想开发一个应用(1-12)
    如果你想开发一个应用(1-11)
  • 原文地址:https://www.cnblogs.com/allyh/p/8993174.html
Copyright © 2011-2022 走看看