首页 > 代码库 > CameraControl

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;
         }  
     }
}

 

CameraControl