zoukankan      html  css  js  c++  java
  • 10. 将摄像机对准物体,并显示整个对准过程,摄像机Zoom

    1. 如果把代码放到按钮事件中调用,达不到想要的效果

    2. 可以不用委托,但是要在Update函数中写调用CameraZoonIn的代码

    3. 有很多需要改进的地方,可以参考使用 iTween 插件达到更好的效果

    using UnityEngine;
    using System.Collections;
    
    public class test : MonoBehaviour
    {
    
        public delegate void dgCameraCompleted(Vector3 pos, Vector3 rot);
        public dgCameraCompleted CameraCompleted;
    
        public Camera currentCamera; //摄像机
    
        public static float originFov;  //源视野
    
        private  Vector3 posStart;
        private  Vector3 rotStart;
        private static Vector3 posEnd;
        private static Vector3 rotEnd;
        private static Vector3 dPos;
        private static Vector3 dRot;
    
        public static bool cameraIsMoving = false;
        // 摄像机当前的状态 zoom in or out
        public static bool cameraIsZoomIn = false;
    
        public static float minFov = 5f;
        public static float maxFov = 90f;
        public static float sensitivity = 10f;
        private int i = 0;
    
        // 通过帧数控制摄像机移动的速度
        private static int closeFrames = 70;
    
        Vector3 posBe, rotBe;
        private static Vector3 localPositionOrigin;
        private static Vector3 localRotationOrigin;
    
        public GameObject target;
        void Start()
        {
            localPositionOrigin = currentCamera.transform.localPosition;
            localRotationOrigin = currentCamera.transform.localEulerAngles;
    
            originFov = currentCamera.fieldOfView;
           
            //目标位置
            posBe = target.transform.position + new Vector3(-0.06f, 0.38f, -0.06f);
            rotBe = new Vector3(94f, 270f, 0f);
        }
    
        private void Update()
        {
            if (CameraCompleted != null)
            {
                CameraCompleted(posBe, rotBe);
    
            }
            // 照相机从源到目标
            if (cameraIsZoomIn)
            {
                if (cameraIsMoving)
                {
                    i++;
                    // 摄像机每帧移动固定的位置和角度,closeFrames帧后到达目标位置
                    currentCamera.transform.position += dPos;
                    currentCamera.transform.eulerAngles += dRot;
    
                    if (i == closeFrames)
                    {
                        cameraIsMoving = false;
                        i = 0;
                    }
                }
            } // 通过滑轮控制视野大小
            else
            {
                float fov = currentCamera.fieldOfView;
    
                fov -= Input.GetAxis("Mouse ScrollWheel") * sensitivity;
                fov = Mathf.Clamp(fov, minFov, maxFov);
    
                currentCamera.fieldOfView = fov;
            }
    
        }
        public void CameraZoomIn(Vector3 posTar, Vector3 rotTar)
        {
            currentCamera.fieldOfView = originFov;
    
            posEnd = posTar;
            rotEnd = rotTar;
            Debug.Log("posTar=" + posTar);
            Debug.Log("rotTar=" + rotTar);
    
            posStart = currentCamera.transform.position;
    
            dPos = new Vector3((posEnd.x - posStart.x) / closeFrames, (posEnd.y - posStart.y) / closeFrames, (posEnd.z - posStart.z) / closeFrames);
    
            rotStart = currentCamera.transform.eulerAngles;
            dRot = (rotEnd - rotStart) / closeFrames;
    
            cameraIsMoving = true;
            cameraIsZoomIn = true;
    
            FixCameraView(true);
        }
    
        private static void FixCameraView(bool bFixed)
        {
            // 如果有地方用到了摄像机,需要进行修正
            if (cameraIsZoomIn == bFixed)
            {
                //mainCamera.transform.parent.GetComponent<CharacterMotor>().enabled = !bFixed;
                //mainCamera.GetComponent<MouseLook>().enabled = !bFixed;
                //mainCamera.transform.parent.GetComponent<MouseLook>().enabled = !bFixed;
            }
        }
    
        void OnGUI()
        {
            if (GUI.Button(new Rect(10, 10, 40, 40), "开始"))
            {
                CameraCompleted += CameraZoomIn;
                Debug.Log(CameraCompleted);
            }
            if (GUI.Button(new Rect(10, 55, 40, 40), "结束"))
            {
                CameraZoomOut();
                CameraCompleted = null;
            }
        }
    
        public void CameraZoomOut()
        {
            if (currentCamera == null)
                Debug.Log("camera");
            if (localPositionOrigin == null)
                Debug.Log("localposition");
            currentCamera.transform.localPosition = localPositionOrigin;
            currentCamera.transform.localEulerAngles = localRotationOrigin;
            cameraIsMoving = false;
            cameraIsZoomIn = false;
            FixCameraView(false);
        }
    
    }
    

      

  • 相关阅读:
    Codeforces 526D Om Nom and Necklace (KMP)
    HDU
    HDU
    Codeforces 219D
    HDU
    HDU
    POJ
    HDU
    HDU
    第二次作业
  • 原文地址:https://www.cnblogs.com/code1992/p/3652072.html
Copyright © 2011-2022 走看看