在Unity3D中實現攝像機跟隨的三種方法


在設計第一人稱射擊游戲以及RPG游戲時,往往需要在主角身上或者近鄰位置設置一個攝像機,使其能夠跟隨主角的移動,提升游戲體驗,這里介紹三種實現攝像機跟隨的方法。

       (一)固定攝像機方法,常用於RPG游戲

第一種方法,在Unity的坐標系中,我將攝像機固定在主角頭部上邊靠后位置,這樣,主角在移動過程中,攝像機也隨着移動,最后在游戲場景中大概是這個樣子:

        也就是說,攝像機相對於主角,總是在Vector3.forward方向上靠后一些,在Vector3.up方向上偏上一些。同時為了使攝像機的移動更加平滑,避免掉幀現象,引入差值函數Vector3.Lerp(),使攝像機的移動更加圓潤。

         相關代碼如下:

  1.  
    using UnityEngine;
  2.  
    using System.Collections;
  3.  
     
  4.  
    /// <summary>
  5.  
    /// Third person camera.
  6.  
    /// </summary>
  7.  
    public class TheThirdPersonCamera : MonoBehaviour
  8.  
    {
  9.  
    public float distanceAway=1.7f;
  10.  
    public float distanceUp=1.3f;
  11.  
    public float smooth=2f; // how smooth the camera movement is
  12.  
     
  13.  
    private Vector3 m_TargetPosition; // the position the camera is trying to be in)
  14.  
     
  15.  
    Transform follow; //the position of Player
  16.  
     
  17.  
    void Start(){
  18.  
    follow = GameObject.FindWithTag ( "Player").transform;
  19.  
    }
  20.  
     
  21.  
    void LateUpdate ()
  22.  
    {
  23.  
    // setting the target position to be the correct offset from the
  24.  
    m_TargetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;
  25.  
     
  26.  
    // making a smooth transition between it's current position and the position it wants to be in
  27.  
    transform.position = Vector3.Lerp(transform.position, m_TargetPosition, Time.deltaTime * smooth);
  28.  
     
  29.  
    // make sure the camera is looking the right way!
  30.  
    transform.LookAt(follow);
  31.  
    }
  32.  
    }

     (二)攝像機替代主角方法,常用於第一人稱射擊

       如圖所示,直接用攝像機替代主角視角,攝像機看到的便是玩家在游戲場景中看到的。首先在Hierachy視圖中建立空物體作為Player,使其旋轉方向與攝像機保持一致。

        相關代碼如下:

// 攝像機Transform
 
  1.  
    Transform m_camTransform;
  2.  
     
  3.  
    // 攝像機旋轉角度
  4.  
    Vector3 m_camRot;
  5.  
     
  6.  
    // 攝像機高度(即表示主角的身高)
  7.  
    float m_camHeight = 1.4f;
  1.  
    void Start () {
  2.  
     
  3.  
    m_transform = this.transform;
  4.  
     
  5.  
     
  6.  
    // 獲取攝像機
  7.  
    m_camTransform = Camera.main.transform;
  8.  
     
  9.  
    // 設置攝像機初始位置
  10.  
    m_camTransform.position = m_transform.TransformPoint( 0, m_camHeight, 0); //攝像機初始位置從本地坐標轉化成世界坐標,且在X-Z平面的初始位置
  11.  
     
  12.  
    // 設置攝像機的旋轉方向與主角一致
  13.  
    m_camTransform.rotation = m_transform.rotation; //rotation為物體在世界坐標中的旋轉角度,用Quaternion賦值
  14.  
    m_camRot = m_camTransform.eulerAngles; //在本游戲實例中用歐拉角表示旋轉
  15.  
    }

 

  1.  
    void Control()
  2.  
        {
  3.  
     
  4.  
            //獲取鼠標移動距離
  5.  
            float rh = Input.GetAxis("Mouse X");
  6.  
            float rv = Input.GetAxis("Mouse Y");
  7.  
     
  8.  
            // 旋轉攝像機
  9.  
            m_camRot.x -= rv;
  10.  
            m_camRot.y += rh;
  11.  
            m_camTransform.eulerAngles = m_camRot; //通過改變XYZ軸的旋轉改變歐拉角
  12.  
     
  13.  
            // 使主角的面向方向與攝像機一致
  14.  
            Vector3 camrot = m_camTransform.eulerAngles;
  15.  
            camrot.x = 0; camrot.z = 0;
  16.  
            m_transform.eulerAngles = camrot;
    }
 

      (三)類似於第一種方法

此方法在學習制作一款坦克大戰時用到,原理其實和第一種方法類似,只不過用到了正余弦函數。

    相關代碼如下:

  1.  
    public float distance = 8;
  2.  
    //橫向角度
  3.  
    public float rot = 0; //用弧度表示
  4.  
    //縱向角度
  5.  
    private float roll = 30f * Mathf.PI * 2 / 360; //弧度
  6.  
    //目標物體
  7.  
    private GameObject target;
  1.  
    void Start()
  2.  
    {
  3.  
    //找到坦克
  4.  
    target = GameObject.Find( "Tank");
  5.  
     
  6.  
    }
 
[html] view plain copy
 
 
print?
  1. <code class="language-csharp">void LateUpdate()   
  2.     {  
  3.         //一些判斷  
  4.         if (target == null)  
  5.             return;  
  6.         if (Camera.main == null)  
  7.             return;  
  8.         //目標的坐標  
  9.         Vector3 targetPos = target.transform.position;  
  10.         //用三角函數計算相機位置  
  11.         Vector3 cameraPos;  
  12.         float d = distance *Mathf.Cos (roll);  
  13.         float height = distance * Mathf.Sin(roll);  
  14.         cameraPos.x = targetPos.x +d * Mathf.Cos(rot);  
  15.         cameraPos.z = targetPos.z + d * Mathf.Sin(rot);  
  16.         cameraPos.y = targetPos.y + height;  
  17.         Camera.main.transform.position = cameraPos;  
  18.         //對准目標  
  19.         Camera.main.transform.LookAt(target.transform);  
  20.      }</code>  


免責聲明!

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



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