在设计第一人称射击游戏以及RPG游戏时,往往需要在主角身上或者近邻位置设置一个摄像机,使其能够跟随主角的移动,提升游戏体验,这里介绍三种实现摄像机跟随的方法。

       (一)固定摄像机方法,常用于RPG游戏

unity中相机跟随 unity摄像头跟随_旋转角度

第一种方法,在Unity的坐标系中,我将摄像机固定在主角头部上边靠后位置,这样,主角在移动过程中,摄像机也随着移动,最后在游戏场景中大概是这个样子:

unity中相机跟随 unity摄像头跟随_世界坐标_02

        也就是说,摄像机相对于主角,总是在Vector3.forward方向上靠后一些,在Vector3.up方向上偏上一些。同时为了使摄像机的移动更加平滑,避免掉帧现象,引入差值函数Vector3.Lerp(),使摄像机的移动更加圆润。

         相关代码如下:

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

     (二)摄像机替代主角方法,常用于第一人称射击

       如图所示,直接用摄像机替代主角视角,摄像机看到的便是玩家在游戏场景中看到的。首先在Hierachy视图中建立空物体作为Player,使其旋转方向与摄像机保持一致。

unity中相机跟随 unity摄像头跟随_旋转角度_03

        相关代码如下:

// 摄像机Transform

 

}
}
1.  
Transform m_camTransform;
2.   
3.  
// 摄像机旋转角度
4.  
Vector3 m_camRot;
5.   
6.  
// 摄像机高度(即表示主角的身高)
7.  
float m_camHeight = 1.4f;
8.  
void Start () {
9.   
10.  
m_transform = this.transform;
11.   
12.   
13.  
// 获取摄像机
14.  
m_camTransform = Camera.main.transform;
15.   
16.  
// 设置摄像机初始位置
17.  
m_camTransform.position = m_transform.TransformPoint(0, m_camHeight, 0); //摄像机初始位置从本地坐标转化成世界坐标,且在X-Z平面的初始位置
18.   
19.  
// 设置摄像机的旋转方向与主角一致
20.  
m_camTransform.rotation = m_transform.rotation; //rotation为物体在世界坐标中的旋转角度,用Quaternion赋值
21.  
m_camRot = m_camTransform.eulerAngles; //在本游戏实例中用欧拉角表示旋转
22.  
}
 
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.  
//通过改变XYZ轴的旋转改变欧拉角
12.   
13.  
// 使主角的面向方向与摄像机一致
14.  
        Vector3 camrot = m_camTransform.eulerAngles;
15.  
0; camrot.z = 0;
16.  
        m_transform.eulerAngles = camrot;
}

 

      (三)类似于第一种方法

此方法在学习制作一款坦克大战时用到,原理其实和第一种方法类似,只不过用到了正余弦函数。

unity中相机跟随 unity摄像头跟随_旋转角度_04

unity中相机跟随 unity摄像头跟随_unity中相机跟随_05

    相关代码如下:

1.  
2.  
//横向角度
3.  
public float rot = 0; //用弧度表示
4.  
//纵向角度
5.  
private float roll = 30f * Mathf.PI * 2 / 360; //弧度
6.  
//目标物体
7.  
private GameObject target;
8.  
void Start()
9.  
{
10.  
//找到坦克
11.  
target = GameObject.Find("Tank");
12.   
13.  
}
1. <code class="language-csharp">void LateUpdate()   
2.     {  
3.         //一些判断  
4. target == null)  
5.             return;  
6. Camera.main == null)  
7.             return;  
8.         //目标的坐标  
9. targetPos = target.transform.position;  
10.         //用三角函数计算相机位置  
11.         Vector3 cameraPos;  
12. d = distance *Mathf.Cos (roll);  
13. 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>