中文字幕无码a片久久,亚洲日韩在线观看浪潮,人人超人人超碰超国产二区,国产人妻久久一区二区,国产人妻久久一区二区

福感科技有限公司 歡迎您!
聯(lián)系方式

    地址:北京市平谷區(qū)馬坊鎮(zhèn)金河北街17號(hào)院3號(hào)樓7層712

    電話:010-89968230

    網(wǎng)站:http://0743119.com

Unity 3d里的相機(jī)跟隨

2021-2-25 19:05:22??????點(diǎn)擊:

固定相機(jī)跟隨

這種相機(jī)有一個(gè)參考對(duì)象,它會(huì)保持與該參考對(duì)象固定的位置,跟隨改參考對(duì)象發(fā)生移動(dòng)

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


固定相機(jī)跟隨,帶有角度旋轉(zhuǎn)

這一種相機(jī)跟隨是對(duì)第一種相機(jī)跟隨的改進(jìn),在原有基礎(chǔ)上面,添加了跟隨角度的控制

using UnityEngine;
using System.Collections;

public class CameriaTrack : MonoBehaviour {
    private Vector3 offset = new Vector3(0,5,4);//相機(jī)相對(duì)于玩家的位置
    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);//調(diào)整相機(jī)與玩家之間的距離
        Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position);//獲取旋轉(zhuǎn)角度
        this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime);

    }

}


第三人稱相機(jī)

這種相機(jī)跟隨,是第三人稱角度看向?qū)ο蟮?,也就是一直看向?qū)ο蟮暮竺妫缫恢憋@示玩家的后背

using UnityEngine;
using System.Collections;
//相機(jī)一直拍攝主角的后背
public class CameraFlow : MonoBehaviour {

    public Transform target;

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

    }

    // Update is called once per frame
    void Update () {
       // 鼠標(biāo)軸控制相機(jī)的遠(yuǎn)近
        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()
    {
       //相機(jī)的位置
        Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway;
        transform.position=Vector3.Lerp(transform.position,disPos,Time.deltaTime*smooth);
        //相機(jī)的角度
        transform.LookAt(target.position);
    }
}


相機(jī)跟隨,鼠標(biāo)控制移動(dòng)和縮放

相機(jī)與觀察對(duì)象保持一定距離,可以通過(guò)鼠標(biāo)進(jìn)行上下左右旋轉(zhuǎn),通過(guò)鼠標(biāo)滾輪進(jìn)行放大和縮小操作

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;
    }
    //左右上下移動(dòng)
    private void Rotate()
    {
        if (Input.GetMouseButton(1))
        {
            Vector3 pos = transform.position;
            Vector3 rot = transform.eulerAngles;

            //圍繞原點(diǎn)旋轉(zhuǎn),也可以將Vector3.zero改為 target.position,就是圍繞觀察對(duì)象旋轉(zhuǎn)
            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);
            //控制移動(dòng)范圍
            if (x < 20 || x > 45 || y < 0 || y > 40)
            {
                transform.position = pos;
                transform.eulerAngles = rot;
            }
            //  更新相對(duì)差值
            offset = transform.position - target.position;
        }
    }
}

聲明:文章內(nèi)容整理來(lái)源于網(wǎng)絡(luò),版權(quán)屬于原作者,如有問(wèn)題,請(qǐng)聯(lián)系我們!


Copyright 2019 0743119.com

福感科技有限公司 版權(quán)所有 All Rights Reserved

京ICP備20002031號(hào)

010-89968230