Unity相機的跟隨移動

一、固定相機跟隨

       這種相機有一個參考對象,它會保持與該參考對象固定的位置,跟隨改參考對象發生移動

using UnityEngine;
using System.Collections;

public class CameraFlow : MonoBehaviour
{
    public Transform target;
    private Vector3 offset;
    // Use this for initialization
    void Start()
    {
        offset = target.position - this.transform.position;

    }

    // Update is called once per frame
    void Update()
    {
        this.transform.position = target.position - offset;
    }
}

二、固定相機跟隨,帶有角度旋轉

       這一種相機跟隨是對第一種相機跟隨的改進,在原有基礎上面,添加了跟隨角度的控制。

using UnityEngine;
using System.Collections;

public class CameriaTrack : MonoBehaviour {
    private Vector3 offset = new Vector3(0,5,4);//相機相對於玩家的位置
    private Transform target;
    private Vector3 pos;

    public float speed = 2;

    // Use this for initialization
    void Start () {
        target = GameObject.FindGameObjectWithTag("Player").transform;

    }

    // Update is called once per frame
    void Update () {
        pos = target.position + offset;
        this.transform.position = Vector3.Lerp(this.transform.position, pos, speed*Time.deltaTime);//調整相機與玩家之間的距離
        Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position);//獲取旋轉角度
        this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime);

    }
}

三、第三人稱相機

      這種相機跟隨,是第三人稱角度看向對象的,也就是一直看向對象的後面,如一直顯示玩家的後背。

using UnityEngine;
using System.Collections;
//相機一直拍攝主角的後背
public class CameraFlow : MonoBehaviour {

    public Transform target;


    public float distanceUp=15f;
    public float distanceAway = 10f;
    public float smooth = 2f;//位置平滑移動值
    public float camDepthSmooth = 5f;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
       // 鼠標軸控制相機的遠近
        if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) || Input.mouseScrollDelta.y > 0 && Camera.main.fieldOfView <= 80)
        {
            Camera.main.fieldOfView += Input.mouseScrollDelta.y * camDepthSmooth * Time.deltaTime;
        }

    }

    void LateUpdate()
    {
       //相機的位置
        Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway;
        transform.position=Vector3.Lerp(transform.position,disPos,Time.deltaTime*smooth);
        //相機的角度
        transform.LookAt(target.position);
    }


}

四、相機跟隨,鼠標控制移動和縮放

      相機與觀察對象保持一定距離,可以通過鼠標進行上下左右旋轉,通過鼠標滾輪進行放大和縮小操作

using UnityEngine;
using System.Collections;

public class CameraFlow : MonoBehaviour
{
    public Transform target;
    Vector3 offset;
    // Use this for initialization
    void Start()
    {
        offset = transform.position - target.position;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = target.position + offset;
        Rotate();
        Scale();
    }
    //縮放
    private void Scale()
    {
        float dis = offset.magnitude;
        dis += Input.GetAxis("Mouse ScrollWheel") * 5;
        Debug.Log("dis=" + dis);
        if (dis < 10 || dis > 40)
        {
            return;
        }
        offset = offset.normalized * dis;
    }
    //左右上下移動
    private void Rotate()
    {
        if (Input.GetMouseButton(1))
        {
            Vector3 pos = transform.position;
            Vector3 rot = transform.eulerAngles;

            //圍繞原點旋轉,也可以將Vector3.zero改爲 target.position,就是圍繞觀察對象旋轉
            transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") * 10);
            transform.RotateAround(Vector3.zero, Vector3.left, Input.GetAxis("Mouse Y") * 10);
            float x = transform.eulerAngles.x;
            float y = transform.eulerAngles.y;
            Debug.Log("x=" + x);
            Debug.Log("y=" + y);
            //控制移動範圍
            if (x < 20 || x > 45 || y < 0 || y > 40)
            {
                transform.position = pos;
                transform.eulerAngles = rot;
            }
            //  更新相對差值
            offset = transform.position - target.position;
        }

    }
}

 

發佈了19 篇原創文章 · 獲贊 14 · 訪問量 3214
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章