1. 程式人生 > >U3D相機的控制(第一、第三人稱)

U3D相機的控制(第一、第三人稱)

        在遊戲中,最常見的視角控制就是滑鼠上下左右移動來實現相機的上下左右移動,以及滑鼠滾輪的滾動來實現視角的縮放

下面將介紹這三種功能的實現:

        因為場景是一個三維座標系,所以需要的地方很多,這裡引入幾個變數,distance, roll, rot , distance ,d為相機和主角之間的空間距離,roll為豎截面distance和x-o-y平面的夾角,d為distance在x-o-y平面的投影,即d = distance * cos(roll),rot為投影d的旋轉角度(弧度)計算相機位置的程式碼如下:

       //計算相機的地面投影和相機高度
        float d = distance * Mathf.Cos(roll);
        float height = distance * Mathf.Sin(roll);
        Vector3 cameraPos;
        //計算相機的座標
        cameraPos.x = target.transform.position.x + d * Mathf.Cos(rot);
        cameraPos.z = target.transform.position.z + d * Mathf.Sin(rot);
        cameraPos.y = target.transform.position.y + height;

最後相機要始終朝向主角:

        Camera.main.transform.position = cameraPos;
        Camera.main.transform.LookAt(target.transform.position);

上述程式碼即可實現鏡頭的跟隨,接下來就是要實現滑鼠的調整視角,其實只需要通過獲取軸的操作來使rot(水平轉向) 、roll(垂直轉向)、distance(縮放距離)做改變

即 Input.GetAxis("Mouse X") 、Input.GetAxis("Mouse Y") 、Input.GetAxis("Mouse ScrollWheel") 來改變上面三個值

 

完整程式碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraCtrl : MonoBehaviour {
    //目標物體
    private GameObject target;
    //相機與目標物體的距離
    public float distance;
    //橫向角度
    public float rotDegree = 0;
    public float rotSpeed = 0.2f;
    float rot; //弧度
    //縱向角度
    public float rollDegree = 30;
    public float rollSpeed = 0.2f;
    public float minRollDegree = 0;
    public float maxRollDegree = 70;
    float roll; //弧度
    //攝像機移動速度(滾輪控制)
    public float zoomSpeed = 1.5f;
    public float minDis = 8;
    public float maxDis = 22;

    // Use this for initialization
    void Start () {
        target = GameObject.Find("tank");
        GetTarget(target);
    }
    
    // Update is called once per frame
    void Update () {
        Rotate();
        Roll();
        Zoom();
        //角度轉換成弧度
        rot = rotDegree * Mathf.PI / 180;
        roll = rollDegree * Mathf.PI / 180;
        //計算相機的地面投影和相機高度
        float d = distance * Mathf.Cos(roll);
        float height = distance * Mathf.Sin(roll);
        Vector3 cameraPos;
        //計算相機的座標
        cameraPos.x = target.transform.position.x + d * Mathf.Cos(rot);
        cameraPos.z = target.transform.position.z + d * Mathf.Sin(rot);
        cameraPos.y = target.transform.position.y + height;
        Camera.main.transform.position = cameraPos;
        Camera.main.transform.LookAt(target.transform.position);
    }
    //滑鼠水平滑動調整視角
    void Rotate() {
        float value = Input.GetAxis("Mouse X");
        rotDegree -= value * rotSpeed;
    }
    //滑鼠垂直方向調整視角
    void Roll() {
        float value = Input.GetAxis("Mouse Y");
        rollDegree += value * rollSpeed;
        rollDegree = Mathf.Clamp(rollDegree, minRollDegree, maxRollDegree);
    }
    //滾輪調整視角的遠近
    void Zoom() {
        float value = Input.GetAxis("Mouse ScrollWheel");
        distance -= value * zoomSpeed;
        distance = Mathf.Clamp(distance, minDis, maxDis);
    }

    //讓相機始終跟隨目標物體身上的某一點
    void GetTarget(GameObject target) {
        if (target.transform.Find("cameraPoint") != null)
        {
            this.target = target.transform.Find("cameraPoint").gameObject;
        }
        else {
            return;
        }
    }
}