unity摄像机旋转带有滑动效果(自转)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 using UnityEngine.EventSystems;

public class Cameratest : MonoBehaviour { 

public static Cameratest instance;
private void Awake()
{
    instance = this;
}
public float sensitivity = 4.0f;
[HideInInspector]
public float sensitivityAmt = 4.0f;
private float minimumX = -360f;
private float maximumX = 360f;
private float minimumY = -85f;
private float maximumY = 85f;
[HideInInspector]
public float rotationX = 0.0f;
[HideInInspector]
public float rotationY = 0.0f;
[HideInInspector]
public float inputY = 0.0f;

public float smoothSpeed = 0.35f;

private Quaternion originalRotation;
private Transform myTransform;
[HideInInspector]
public float recoilX;
[HideInInspector]
public float recoilY;
//判断是否需要旋转
private bool m_IsonClickUI;
//摄像机的视角大小放大缩小功能
private float m_cameraFieldOfMask=60;
void Start()
{
    myTransform = transform;
    originalRotation = myTransform.localRotation;      
    Vector3 tempRotation = new Vector3(0, Camera.main.transform.eulerAngles.y, 0);
    originalRotation.eulerAngles = tempRotation;
    sensitivityAmt = sensitivity;
}

void Update()
{
    Ispointetovergameobject(Input.mousePosition);
    RotationCamera();
    ZoomInOut();
}
/// 
/// 初始化相机角度
/// 
/// 
/// 
public void InitRot(float Angle_x,float Angle_y)
{
    rotationX = Angle_x;
    rotationY = Angle_y;
}
/// 
/// 相机旋转
/// 
void RotationCamera()
{
    if (!m_IsonClickUI)
    {
        if (Input.GetMouseButton(0))
        {
            Cursor.visible = false;
            if (Time.timeScale > 0 && Time.deltaTime > 0)
            {
                rotationX += Input.GetAxisRaw("Mouse X") * -sensitivityAmt * Time.timeScale;
                rotationY += Input.GetAxisRaw("Mouse Y") * -sensitivityAmt * Time.timeScale;

                //reset vertical recoilY value if it would exceed maximumY amount 
                if (maximumY - Input.GetAxisRaw("Mouse Y") * sensitivityAmt * Time.timeScale < recoilY)
                {
                    rotationY += recoilY;
                    recoilY = 0.0f;
                }
                //reset horizontal recoilX value if it would exceed maximumX amount 
                if (maximumX - Input.GetAxisRaw("Mouse X") * sensitivityAmt * Time.timeScale < recoilX)
                {
                    rotationX += recoilX;
                    recoilX = 0.0f;
                }
                rotationX = ClampAngle(rotationX, minimumX, maximumX);
                rotationY = ClampAngle(rotationY, minimumY - recoilY, maximumY - recoilY);
                inputY = rotationY + recoilY;
            }           
        }
        else
        {
            Cursor.visible = true;
        }
    }
    if (Input.GetMouseButtonUp(0))
    {
        m_IsonClickUI = false;
    }
    Quaternion xQuaternion = Quaternion.AngleAxis(rotationX + recoilX, Vector3.up);
    Quaternion yQuaternion = Quaternion.AngleAxis(rotationY + recoilY, -Vector3.right);
    // myTransform.rotation = Quaternion.Slerp(myTransform.rotation, originalRotation * xQuaternion * yQuaternion, smoothSpeed * Time.smoothDeltaTime * 60 / Time.timeScale);
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation, originalRotation * xQuaternion * yQuaternion, smoothSpeed * Time.deltaTime);
    myTransform.rotation = Quaternion.Euler(myTransform.rotation.eulerAngles.x, myTransform.rotation.eulerAngles.y, 0.0f);
}

/// 
/// 限制旋转角度
/// 
/// 
/// 
/// 
/// 
public static float ClampAngle(float angle, float min, float max)
{
    angle = angle % 360;
    if ((angle >= -360F) && (angle <= 360F))
    {
        if (angle < -360F)
        {
            angle += 360F;
        }
        if (angle > 360F)
        {
            angle -= 360F;
        }
    }
    return Mathf.Clamp(angle, min, max);
}
/// 
/// 放大缩小
/// 
void ZoomInOut()
{
    //鼠标放大缩小功能
    if (Input.GetAxis("Mouse ScrollWheel") < 0)
    {
        m_cameraFieldOfMask = Mathf.Clamp(m_cameraFieldOfMask += 2, 30, 60);
        Camera.main.fieldOfView = m_cameraFieldOfMask;
    }
    else if (Input.GetAxis("Mouse ScrollWheel") > 0)
    {
        m_cameraFieldOfMask = Mathf.Clamp(m_cameraFieldOfMask -= 2, 30, 60);
        Camera.main.fieldOfView = m_cameraFieldOfMask;
    }
}
/// 
/// 判断是否点击到UI
/// 
/// 
void Ispointetovergameobject(Vector2 input)
{
    PointerEventData eventData = new PointerEventData(EventSystem.current);
    eventData.position = input;
    List rayresults = new List();
    EventSystem.current.RaycastAll(eventData, rayresults);
    if (Input.GetMouseButtonDown(0))
    {
        for (int i = 0; i < rayresults.Count; i++)
        {
            if (rayresults[i].gameObject.tag == "ui")
            {
                m_IsonClickUI = true;
            }
        }
    }
}
}

复制脚本放到摄像机上即可

你可能感兴趣的:(unity)