【Unity】小的相機控制腳本,用於展示類小程序

     小項目的腳本,可用於焦點相機,沒啥技術含量,爲了以後省事就記下來了。

     測試可用的代碼,用的時候把腳本掛在相機上,然後創建一個空對象取名叫“FocusObj”就可以了,需要的可以試試看

using UnityEngine;
using System.Collections;
using System;

public class FocusCamera : MonoBehaviour
{

    //-----------------  初始參數  -----------------
    private Transform Tanks;
    private float distance;
    private Vector3 targetPos;
    //--旋轉速度
    private float xSpeed = 50.0f;
    private float ySpeed = 50.0f;
    //旋轉限制
    private float rotateMaxX = 90;//最大旋轉A軸,包含正負

    //旋轉記錄
    private float angleX;
    private float angleY;

    //--縮放速度
    private float wheelSpeed = 10;
    
    //--縮放限制,最大,最小
    private float MaxWheel = 8;
    private float MinWheel = 0.5f;

    //--移動限制
    private float moveSpeed = 2;
    //y軸 最大值、最小值
    private float xMaxMove = 1.5f;
    private float xMinMove = -1.5f;
    private float yMaxMove = 4.8f;
    private float yMinMove = 0;
    private float zMaxMove = 2.5f;
    private float zMinMove = 0;

    float lerpRate = 0.2f;
    //臨時變量
    Vector3 t_Vec3;
    //-----------------  系統函數  -----------------
    void Start()
    {
        //初始化中心模塊
        Tanks = GameObject.Find("FocusObj").transform;
        //初始化相機與中心模型的距離
        distance = 5;
        //初始化中心物體位置
        t_Vec3 = Vector3.zero;
        t_Vec3.y = 2.3f;
        t_Vec3.z = 1.2f;
        Tanks.position = t_Vec3;
        //初始化中心物體角度
        Tanks.rotation = Quaternion.identity;

        targetPos = Tanks.position - Tanks.forward * distance;
        //初始化相機位置
        transform.position = targetPos;
        //初始化相機角度
        transform.rotation = Tanks.rotation;
    }
    private void LateUpdate()
    {
        //如果沒有得到中心模塊則此腳本無效
        if (Tanks == null) return;
        //拖動
        if (Input.GetMouseButton(2)||Input.GetKey(KeyCode.LeftControl))
        {
            t_Vec3 = Time.deltaTime * Input.GetAxis("Mouse X") * moveSpeed * distance * Tanks.right;
            t_Vec3 += Time.deltaTime * Input.GetAxis("Mouse Y") * moveSpeed * distance * Tanks.up;

            t_Vec3 = Tanks.position - t_Vec3;
            t_Vec3.x = Mathf.Clamp(t_Vec3.x, xMinMove, xMaxMove);
            t_Vec3.y = Mathf.Clamp(t_Vec3.y, yMinMove, yMaxMove);
            t_Vec3.z = Mathf.Clamp(t_Vec3.z, zMinMove, zMaxMove);

            Tanks.position = t_Vec3;
            targetPos = Tanks.position - Tanks.forward * distance;
        }
        else if (Input.GetMouseButton(1))
        {
            //旋轉
            t_Vec3.x = angleX - Time.deltaTime * Input.GetAxis("Mouse Y") * ySpeed * distance;
            t_Vec3.y = angleY + Time.deltaTime * Input.GetAxis("Mouse X") * xSpeed * distance;
            if (t_Vec3.x > rotateMaxX)
                t_Vec3.x = rotateMaxX;
            else if (t_Vec3.x < -rotateMaxX)
                t_Vec3.x = -rotateMaxX;
            if (t_Vec3.y > 360)
                t_Vec3.y -= 360;
            else if (t_Vec3.y < -360)
                t_Vec3.y += 360;
            angleY = t_Vec3.y;
            angleX = t_Vec3.x;
            Tanks.rotation = Quaternion.Euler(angleX, angleY, 0);

            //刷新目標位置
            targetPos = Tanks.position - Tanks.forward * distance;
        }
        else if(Input.GetKeyDown(KeyCode.Escape))
        {
            //Debug.Log("退出展示程序");
            Application.Quit();
        }
        else
        {
            //縮進
            t_Vec3.x = Input.GetAxis("Mouse ScrollWheel");
            if (t_Vec3.x != 0)
            {
                t_Vec3.x *= Time.deltaTime * wheelSpeed * distance;
                t_Vec3.x += distance;
                if (t_Vec3.x > MaxWheel)
                    distance = MaxWheel;
                else if (t_Vec3.x < MinWheel)
                    distance = MinWheel;
                else
                    distance = t_Vec3.x;

                //刷新目標位置
                targetPos = Tanks.position - Tanks.forward * distance;
            }
        }

        transform.rotation = Quaternion.Lerp(transform.rotation, Tanks.rotation, lerpRate);
        transform.position = Vector3.Lerp(transform.position, targetPos, lerpRate);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章