在設計第一人稱射擊游戲以及RPG游戲時,往往需要在主角身上或者近鄰位置設置一個攝像機,使其能夠跟隨主角的移動,提升游戲體驗,這里介紹三種實現攝像機跟隨的方法。
(一)固定攝像機方法,常用於RPG游戲
第一種方法,在Unity的坐標系中,我將攝像機固定在主角頭部上邊靠后位置,這樣,主角在移動過程中,攝像機也隨着移動,最后在游戲場景中大概是這個樣子:
也就是說,攝像機相對於主角,總是在Vector3.forward方向上靠后一些,在Vector3.up方向上偏上一些。同時為了使攝像機的移動更加平滑,避免掉幀現象,引入差值函數Vector3.Lerp(),使攝像機的移動更加圓潤。
相關代碼如下:
-
using UnityEngine;
-
using System.Collections;
-
-
/// <summary>
-
/// Third person camera.
-
/// </summary>
-
public class TheThirdPersonCamera : MonoBehaviour
-
{
-
public float distanceAway=1.7f;
-
public float distanceUp=1.3f;
-
public float smooth=2f; // how smooth the camera movement is
-
-
private Vector3 m_TargetPosition; // the position the camera is trying to be in)
-
-
Transform follow; //the position of Player
-
-
void Start(){
-
follow = GameObject.FindWithTag ( "Player").transform;
-
}
-
-
void LateUpdate ()
-
{
-
// setting the target position to be the correct offset from the
-
m_TargetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;
-
-
// making a smooth transition between it's current position and the position it wants to be in
-
transform.position = Vector3.Lerp(transform.position, m_TargetPosition, Time.deltaTime * smooth);
-
-
// make sure the camera is looking the right way!
-
transform.LookAt(follow);
-
}
-
}
(二)攝像機替代主角方法,常用於第一人稱射擊
如圖所示,直接用攝像機替代主角視角,攝像機看到的便是玩家在游戲場景中看到的。首先在Hierachy視圖中建立空物體作為Player,使其旋轉方向與攝像機保持一致。
相關代碼如下:
// 攝像機Transform
-
Transform m_camTransform;
-
-
// 攝像機旋轉角度
-
Vector3 m_camRot;
-
-
// 攝像機高度(即表示主角的身高)
-
float m_camHeight = 1.4f;
-
void Start () {
-
-
m_transform = this.transform;
-
-
-
// 獲取攝像機
-
m_camTransform = Camera.main.transform;
-
-
// 設置攝像機初始位置
-
m_camTransform.position = m_transform.TransformPoint( 0, m_camHeight, 0); //攝像機初始位置從本地坐標轉化成世界坐標,且在X-Z平面的初始位置
-
-
// 設置攝像機的旋轉方向與主角一致
-
m_camTransform.rotation = m_transform.rotation; //rotation為物體在世界坐標中的旋轉角度,用Quaternion賦值
-
m_camRot = m_camTransform.eulerAngles; //在本游戲實例中用歐拉角表示旋轉
-
}
-
void Control()
-
{
-
-
//獲取鼠標移動距離
-
float rh = Input.GetAxis("Mouse X");
-
float rv = Input.GetAxis("Mouse Y");
-
-
// 旋轉攝像機
-
m_camRot.x -= rv;
-
m_camRot.y += rh;
-
m_camTransform.eulerAngles = m_camRot; //通過改變XYZ軸的旋轉改變歐拉角
-
-
// 使主角的面向方向與攝像機一致
-
Vector3 camrot = m_camTransform.eulerAngles;
-
camrot.x = 0; camrot.z = 0;
-
m_transform.eulerAngles = camrot;
}
(三)類似於第一種方法
此方法在學習制作一款坦克大戰時用到,原理其實和第一種方法類似,只不過用到了正余弦函數。
相關代碼如下:
-
public float distance = 8;
-
//橫向角度
-
public float rot = 0; //用弧度表示
-
//縱向角度
-
private float roll = 30f * Mathf.PI * 2 / 360; //弧度
-
//目標物體
-
private GameObject target;
-
void Start()
-
{
-
//找到坦克
-
target = GameObject.Find( "Tank");
-
-
}
- <code class="language-csharp">void LateUpdate()
- {
- //一些判斷
- if (target == null)
- return;
- if (Camera.main == null)
- return;
- //目標的坐標
- Vector3 targetPos = target.transform.position;
- //用三角函數計算相機位置
- Vector3 cameraPos;
- float d = distance *Mathf.Cos (roll);
- float height = distance * Mathf.Sin(roll);
- cameraPos.x = targetPos.x +d * Mathf.Cos(rot);
- cameraPos.z = targetPos.z + d * Mathf.Sin(rot);
- cameraPos.y = targetPos.y + height;
- Camera.main.transform.position = cameraPos;
- //對准目標
- Camera.main.transform.LookAt(target.transform);
- }</code>