zoukankan      html  css  js  c++  java
  • CameraControl

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class CameraControl : MonoBehaviour
    {
        public Transform center;
        private Vector3 Center;
    
        //ScrollWheel
        private int MouseWheelSensitivity = 2;
        private int MouseZoomMin = 1;         
        private int MouseZoomMax = 200;         
        public float normalDistance = 6;     
        private Vector3 normalized;
        private Vector3 temp = new Vector3(0,0,0);
    
        //Rotation
        private float xSpeed = 250.0f;
        private float ySpeed = 120.0f;
        private int yMinLimit = 0;
        private int yMaxLimit = 80;
        public bool needDamping = false; //调整摄像机
        private float damping = 5.0f;
    
        private float x = 0.0f;
        private float y = 0.0f;
    
        private float movingSpeed = 0.5f;
    
        private Quaternion rotation = Quaternion.Euler(new Vector3(30f, 0f, 0f));
     
        void Start()
        {
            Center = center.position;
    
            float z = center.transform.position.z - normalDistance;
            //transform.position = rotation * new Vector3(transform.position.x, transform.position.y, z);
            transform.LookAt(center);
    
            var angles = transform.eulerAngles;
            x = angles.y;
            y = angles.x;
        }
    
        void LateUpdate()
        {
            Center = center.position;
            //左键旋转
            if (Input.GetMouseButton(0)) RotationCamera();
            //滚轮缩放
            else if (Input.GetAxis("Mouse ScrollWheel") != 0) MouseWheel();
            //中键平移
            //else if (Input.GetMouseButton(2)) HVMoveCamera();
        }
         float ClampAngle(float angle, float min, float max)
        {
            if (angle < -360)
                angle += 360;
            if (angle > 360)
                angle -= 360;
            return Mathf.Clamp(angle, min, max);
        }
         public void RotationCamera()
        {
            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
    
            y = ClampAngle(y, yMinLimit, yMaxLimit);
    
            var rotation = Quaternion.Euler(y, x, 0);
            var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + Center;
            if (needDamping)
            {
                transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
                transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
            }
            else
            {
                transform.rotation = rotation;
                transform.position = position;
            }  
        }
         public void MouseWheel()
         {
             normalized = (Center - transform.position).normalized;
             if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)
             {
                 normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
                 temp = Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity * normalized;
             }
             if (normalDistance < MouseZoomMin)
             {
                 normalDistance = MouseZoomMin;
                 temp.Set(0, 0, 0);
             }
             if (normalDistance > MouseZoomMax)
             {
                 normalDistance = MouseZoomMax;
                 temp.Set(0, 0, 0);
             }
             transform.position += temp;
         }
         public void HVMoveCamera()
         {
             x = Input.GetAxis("Mouse X") * movingSpeed;
             y = Input.GetAxis("Mouse Y") * movingSpeed;
             rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
             transform.position = rotation * new Vector3(x, 0, y) + transform.position;
         }
        //模拟单击事件
         public void TranslateCamera(Vector3 cen)
         {
             x = cen.x;
             y = cen.y;
             y = ClampAngle(y, yMinLimit, yMaxLimit);
    
             var rotation = Quaternion.Euler(y, x, 0);
             var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + Center;
             if (needDamping)
             {
                 transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
                 transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
             }
             else
             {
                 transform.rotation = rotation;
                 transform.position = position;
             }  
         }
    }
  • 相关阅读:
    (分享)视频压缩Free Video Compressor 汉化版/中文版【全网唯一】
    (分享)根据IP获取地理位置(百度API)
    易语言5.6 精简破解版[Ctoo]
    性能测试---流程篇
    性能测试--系统资源配置篇
    结合sqlmap进行sql注入过程
    MySQL使用记录
    Oracle创表操作记录
    Oracle常用函数记录
    Oracle使用记录
  • 原文地址:https://www.cnblogs.com/greyhh/p/6736835.html
Copyright © 2011-2022 走看看