镜头的旋转

镜头的旋转

using UnityEngine;
using System.Collections;

public class CameraRotate : MonoBehaviour
{
    public Transform targetObject;
    public Vector3 targetOffset;
    public float averageDistance = 5.0f;
    public float maxDistance = 20;
    public float minDistance = .6f;
    public float xSpeed = 200.0f;
    public float ySpeed = 200.0f;
    public int yMinLimit = -80;
    public int yMaxLimit = 80;
    public int zoomSpeed = 40;
    public float panSpeed = 0.3f;
    public float zoomDampening = 5.0f;
    public float rotateOnOff = 1;

    private float xDeg = 0.0f;
    private float yDeg = 0.0f;
    private float currentDistance;
    private float desiredDistance;
    private Quaternion currentRotation;
    private Quaternion desiredRotation;
    private Quaternion rotation;
    private Vector3 position;
    private float idleTimer = 0.0f;
    private float idleSmooth = 0.0f;
    
    void Start() { Init(); }
    void OnEnable() { Init(); }

    public void Init()
    {
        if (!targetObject)
        {
            GameObject go = new GameObject("Cam Target");
            go.transform.position = transform.position + (transform.forward * averageDistance);
            targetObject = go.transform;
        }

        currentDistance = averageDistance;
        desiredDistance = averageDistance;
        
        position = transform.position;
        rotation = transform.rotation;
        currentRotation = transform.rotation;
        desiredRotation = transform.rotation;
       
        xDeg = Vector3.Angle(Vector3.right, transform.right );
        yDeg = Vector3.Angle(Vector3.up, transform.up );
        position = targetObject.position - (rotation * Vector3.forward * currentDistance + targetOffset);
    }

    void LateUpdate()
    {  
        **//鼠标左键控制摄像机旋转**
        if (Input.GetMouseButton(2) && Input.GetKey(KeyCode.LeftAlt) && Input.GetKey(KeyCode.LeftControl))
        {
            desiredDistance -= Input.GetAxis("Mouse Y") * 0.02f  * zoomSpeed*0.125f * Mathf.Abs(desiredDistance);
        }
        else if (Input.GetMouseButton(0) )
        {
            xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
            yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
            
            desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
            currentRotation = transform.rotation;
            rotation = Quaternion.Lerp(currentRotation, desiredRotation, 0.02f  * zoomDampening);
            transform.rotation = rotation;
            idleTimer=0;
            idleSmooth=0;
       
        }else{
            idleTimer+=0.02f ;
            if(idleTimer > rotateOnOff && rotateOnOff > 0){
                idleSmooth+=(0.02f +idleSmooth)*0.005f;
                idleSmooth = Mathf.Clamp(idleSmooth, 0, 1);
                xDeg += xSpeed * 0.001f * idleSmooth;
            }

            yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
            desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
            currentRotation = transform.rotation;
            rotation = Quaternion.Lerp(currentRotation, desiredRotation, 0.02f  * zoomDampening*2);
            transform.rotation = rotation;
        }

         //**鼠标滑轮控制镜头靠近或拉远**
        desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * 0.02f  * zoomSpeed * Mathf.Abs(desiredDistance);
        desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
        currentDistance = Mathf.Lerp(currentDistance, desiredDistance, 0.02f  * zoomDampening);
        position = targetObject.position - (rotation * Vector3.forward * currentDistance + targetOffset);
        transform.position = position;
    }

    private 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);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 随着摄影器材的普及,摄影正逐渐成为一种流行的文化需求,但是我们多数人对相机和镜头等知识的了解并不深入。我们只知道电...
    一个影像馆阅读 14,293评论 0 1
  • 「相机如何保养」这个议题,满早就有这个念头想要整理一篇了,但一直以来观察到许许多多相关的网站内容,甚至出版的书籍,...
    kjkj88888阅读 2,635评论 0 0
  • 山野匹夫,涉猎未足。先天下忧患,心之怛怛。后天下安乐,心之坦坦。雨霏晴霁,阴风嚎怒,波澜不惊。夫君子者,不安一隅,...
    焱博帅帅科阅读 3,153评论 1 1
  • 这是李笑来老师第40周的分享,认真学习并且思考后,有了自己对速成的理解。 速成绝不可能,但快速入门绝对可能,而且很...
    思行彬阅读 2,515评论 0 0
  • 纪钧:今天不想发口头作文,因为我的好朋友也是我的同桌,上次也发过我的好朋友,也是说的谢若兮,所以不想发了。 妈妈:...
    怡红宝玉阅读 1,432评论 0 0

友情链接更多精彩内容