Unity相機跟隨-----根據速度設置偏移量


這里假設在水中的船,船有慣性,在不添加前進動力的情況下會繼續移動,但是船身是可以360度自由旋轉,當船的運動速度在船的前方的時候,相機會根據向前的速度的大小,設置相機的偏移量,從而提高游戲的動態帶感。

但是由於慣性船的速度不一定在船的,因此可以獲得當前船的速度方向在船的前方的投影分量,當分量與船的前方同向,那么設置偏移量為:速度分量的長度與船的最大比值t,乘以相機設定的最大偏移量

 

代碼1

如下

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

public class CameraControl : MonoBehaviour {

    GameObject player;
    public float speed=10f;

最大偏移量,這里最好能保證角色在屏幕范圍內
public float radius;// Use this for initialization void Start () { player = GameObject.FindWithTag("Player"); } // Update is called once per frame void Update () { Follow(); }void Follow() { if (!player) { player = GameObject.FindWithTag("Player"); } else { Rigidbody2D rigid= player.GetComponent<Rigidbody2D>(); Ship ship = player.GetComponent<Ship>(); Vector3 playerVelocity = rigid.velocity; //求出角色的速度在角色的正方向的分量速度 Vector3 playerVelocityOnForward= Vector3.Project(playerVelocity,player.transform.up); //得到分量速度的方向與正方向是否同符號 float dot = Vector3.Dot(playerVelocityOnForward, player.transform.up); //如果同符號,那么得到它的模與最大速度值的比值,用該比值乘以radius,即可得到相機的偏移位置量;如果不同號,即速度方向相反,那么比例值設置為0 float offsetLength = (dot >= 0 ? playerVelocityOnForward.magnitude / ship.maxSpeed : 0) * radius;
transform.position
= Vector3.Lerp (transform.position, player.transform.position + player.transform.up* offsetLength - Vector3.forward, Time.deltaTime * speed); } } }

 

在代碼1上可以看出,在飛船開動引擎和關閉引擎時,由於速度的劇烈變化,相機也會跟着劇烈變化,玩家眼睛無法跟上畫面的劇烈變化,最好的解決辦法是將radius的變化放緩,從而使得相機的動作流暢,便有了代碼2.

代碼2:

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

public class CameraControl : MonoBehaviour {

    GameObject player;
    public float speed=10f;//相機的最大速度

    public float radius;

    float minRadius=1f;

    float currentRadius;

    // Use this for initialization
    void Start () {
        player = GameObject.FindWithTag("Player");
    }
    
    // Update is called once per frame
    void Update () {
        Follow();
    }void Follow()
    {
        if (!player)
        {
            player = GameObject.FindWithTag("Player");
        }
        else
        {
            Rigidbody2D  rigid= player.GetComponent<Rigidbody2D>();
            Ship ship = player.GetComponent<Ship>();
            Vector3 playerVelocity = rigid.velocity;
            //求出角色的速度在角色的正方向的分量速度
            Vector3 playerVelocityOnForward= Vector3.Project(playerVelocity,player.transform.up);
            //得到分量速度的方向與正方向是否同符號
            float dot = Vector3.Dot(playerVelocityOnForward, player.transform.up);
            //如果同符號,那么得到它的模與最大速度值的比值,用該比值乘以radius,即可得到相機的偏移位置量;如果不同號,即速度方向相反,那么比例值設置為0
            float offsetLength = (dot >= 0 ? playerVelocityOnForward.magnitude / ship.maxSpeed : 0)  *  radius;
            //Debug.Log(offsetLength);
            //如果按下了加速鍵,那么讓相機在角色前方offsetLength處,否則,相機在角色前方最小偏移處
            if (Input.GetKey(KeyCode.UpArrow))
            {
                if (currentRadius <= radius)
                {
                    //currentRadius = currentRadius + Time.deltaTime * 3f;
                    //currentRadius = Mathf.Clamp(currentRadius, minRadius, radius);
                    //currentRadius = offsetLength;
                    currentRadius = Mathf.Lerp(currentRadius, offsetLength, Time.deltaTime * 3);
                };    
            }
            else
            { 
                if (currentRadius > minRadius)
                {
                    currentRadius = currentRadius - Time.deltaTime * 3f;
                }
                currentRadius = Mathf.Clamp(currentRadius, minRadius, radius); 
            }
          transform.position = Vector3.Lerp(transform.position, player.transform.position + player.transform.up * currentRadius - Vector3.forward, Time.deltaTime * speed);
} } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM