unity3d 实现鼠标滑轮控制“镜头远近”的示例

1. 编写CameraControl.cs类

using System.Diagnostics;
using UnityEngine;
using System.Collections;
using Debug = UnityEngine.Debug;

public class CameraControl : MonoBehaviour
{
    public static GameObject mainCamera;

    private static Vector3 localPositionOrigin;
    private static Vector3 localRotationOrigin;
    private static Vector3 rotStart;
    private static Vector3 posStart;
    private static Vector3 posEnd;
    private static Vector3 rotEnd;
    private static Vector3 dPos;
    private static Vector3 dRot;
    public static bool cameraIsMoving = false;
    public static bool cameraIsZoomIn = false; // camera 's current state ( zoom in or out)
    public static string cameraTarget = "";

    public static float minFov = 5f;
    public static float maxFov = 90f;
    public static float sensitivity = 10f;
    public static float originFov;

    private int i = 0;
    private static int closeFrames = 75;

    // Use this for initialization
    private void Start()
    {
        mainCamera = GameObject.Find("Main Camera");
        localPositionOrigin = mainCamera.transform.localPosition;
        localRotationOrigin = mainCamera.transform.localEulerAngles;
        originFov = Camera.main.fieldOfView;
    }

    // Update is called once per frame
    private void Update()
    {
        // camera view zoom in from player to target
        if (cameraIsZoomIn)
        {
            if (cameraIsMoving)
            {
                i++;
                //mainCamera.transform.Rotate(Vector3.up, dRotY);
                mainCamera.transform.eulerAngles += dRot;
                mainCamera.transform.position += dPos;
                //if ((mainCamera.transform.position - posEnd).magnitude < 0.1f)
                if (i == closeFrames)
                {
                    cameraIsMoving = false;
                    i = 0;
                }
            }
        } // camera view zoom in or out while mouse scroll wheel rolling
        else
        {
            float fov = Camera.main.fieldOfView;
            fov -= Input.GetAxis("Mouse ScrollWheel") * sensitivity;
            fov = Mathf.Clamp(fov, minFov, maxFov);
            Camera.main.fieldOfView = fov;
        }

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //从摄像机发出到点击坐标的射线

        RaycastHit hitInfo;
        if (Physics.Raycast(ray, out hitInfo))
        {
            Debug.DrawLine(ray.origin, hitInfo.point); //划出射线,只有在scene视图中才能看到                
            GameObject gameObj = hitInfo.collider.gameObject;
        }
    }

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

    private static int count = 0;
    public static void CameraZoomIn(Vector3 posTar, Vector3 rotTar)
    {
        Camera.main.fieldOfView = originFov;
        //mainCamera.transform.localPosition = localPositionOrigin;
        //mainCamera.transform.localEulerAngles = localRotationOrigin;
        posEnd = posTar;
        rotEnd = rotTar;
        Debug.Log("posTar=" + posTar);
        Debug.Log("rotTar=" + rotTar);
        posStart = mainCamera.transform.position;
        dPos = new Vector3((posEnd.x - posStart.x) / closeFrames, (posEnd.y - posStart.y) / closeFrames, (posEnd.z - posStart.z) / closeFrames);
        //mainCamera.transform.localEulerAngles = localRotationOrigin;
        rotStart = mainCamera.transform.eulerAngles;
        //dRot = (mainCamera.transform.parent.eulerAngles + rotEnd - rotStart) / 100;
        dRot = (rotEnd - rotStart) / closeFrames;
        cameraIsMoving = true;
        cameraIsZoomIn = true;
        FixCameraView(true);
    }

    public static void CameraZoomOut()
    {
        if (mainCamera == null)
            Debug.Log("camera");
        if (localPositionOrigin == null)
            Debug.Log("localposition");
        mainCamera.transform.localPosition = localPositionOrigin;
        mainCamera.transform.localEulerAngles = localRotationOrigin;
        cameraIsMoving = false;
        cameraIsZoomIn = false;
        FixCameraView(false);
    }
}

2. 将脚本赋给摄像机或其它物体

注意:如果有两个摄像机 可能会报错:

There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene.

 

未经允许不得转载:第一Unity3D » unity3d 实现鼠标滑轮控制“镜头远近”的示例

赞 (9)