美文网首页
镜头的旋转

镜头的旋转

作者: ___Reset | 来源:发表于2017-08-15 14:53 被阅读0次

镜头的旋转

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

相关文章

  • 镜头的旋转

    镜头的旋转

  • 镜头运动

    跟随焦点移动 摇:横摇(左右旋转)和直摇(上下旋转) 移: 推轨镜头(左右移动) 人物出场:镜头引导和场景接管 场...

  • three.js 动态标签

    1. 使用材质制作标签 使用材质制作标签的特点是它不会随着镜头旋转而跟随,也就是不会跟着镜头旋转,如下图: 具体代...

  • VR俯冲旋转镜头动画

    最近在开发VR项目,看到这个房产网站的旋转加载动画不错 房产网站加载动画,于是参考这篇全景图转小行星视角投影原理详...

  • c4d部分基础操作

    按住alt键后,滚动滚轮、按住左键拖动、按住滚轮拖动以放缩、旋转镜头、移动镜头。c掉之后,才能使用对象轴模式移动、...

  • 此岸,彼岸

    十二月的杭州,马路两旁飞舞的梧桐树叶,在风中旋转又旋转,不忍离开枝头,2017年就这样走到尽头。 看见镜头前的朴树...

  • 一对夫妇环游世界,彼此互拍产生的美妙瞬间

    Peter Sedlacik 和 Zuzu Galova运用独特的旋转镜头拍摄了他们度假时的照片,其中就包括这次他...

  • 2018/1/21

    镜头一:公园里有个孩子在旋转木马上开心的笑着,爸爸在旁边看着他,一圈又一圈的转着。 镜头二:一个大女孩...

  • 金博洋战胜了金博洋

    芸姐: 傍晚我回看电视冬奥会的花样滑冰节目,正好在播放金博洋空中旋转达到了镜头,你从房里出来喝水,看到了镜头,立刻...

  • 抖音第三课,各种让你抖得不想收手的武林秘诀

    1,随拍,有手机的就行,难度☆☆ 要点: *手不要抖(拖拉机那个除外); *少用旋转式镜头(看了容易晕); *有条...

网友评论

      本文标题:镜头的旋转

      本文链接:https://www.haomeiwen.com/subject/ivxvrxtx.html