這篇文章將為大家詳細講解有關Unity3D實現相機跟隨控制的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
跟隨算法
要實現3D攝像機的控制第一步就是先實現攝像機跟隨物體移動。
要想讓相機跟隨物體移動,就要明白在一定角度下相機與物體的位置關系。
首先設置相機與物體之間的距離distance,相機與xz平面的角度為roll
所以根據三角關系可以求得映射在xz平面的距離d為distancecos(rool),相機高度為distancesin(roll)。
如下圖

現在就可以確定相機的高度了即y軸的坐標相機的y軸坐標應該為 Camera.Main.y=物體.y+height
在xz平面中,設相機與物體的距離為d(就是上面說的那個d,distance映射在xz平面的長度),相機的旋轉角度為rot。根據下圖可以看到,相機與物體的連線與x軸的角度為rot-180.根據三角函數,既可以得出x軸的位移為d*sin(rot) ,z軸的位移為d*cos(rot) 。

所以說開始的時候指定distance和rot和roll就可以實現跟隨了。實現跟隨的代碼如下
public class CameraFollow : MonoBehaviour
{
//距離
public float distance = 15;
//橫向角度
public float rot = 0;
//縱向角度 30d度
public float roll = 30f * Mathf.PI * 2 / 360;
//目標物體
public GameObject target;
private void Start()
{
target = GameObject.Find("Black Track");
}
private 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);
}
}在跟隨的時候我們可以在要跟隨的物體下放置一個子物體命名為cameraPoint使相機對準這個子物體從而方便的更改攝像機的視角。
所以在物體下添加一個cameraPoint的子物體

并且添加代碼
//設置目標
public void SetTarget(GameObject target)
{
if (target.transform.Find("cameraPoint") != null)
this.target = target.transform.Find("cameraPoint").gameObject;
else
this.target = target;
}如果準的物體有名為cameraPoint的子物體,那么相機對準cameraPoint子物體。
橫向與縱向旋轉攝像機
當鼠標向左移動時,相機隨之左轉,當鼠標向右移動時,相機隨之右轉。
Unity的輸入軸Mouse X 和 Mouse Y 代表著鼠標的移動增量,也就是說當鼠標向左移動時,Input.GetAxis(“Mouse X”)的值會增大,向右則減少。只要讓旋轉角度rot與Mouse X成正比關系,便能通過鼠標控制攝像機的角度。
代碼如下
//橫向旋轉速度
public float rotSpeed=0.1f;
//橫向旋轉
public void Rotate()
{
float w = Input.GetAxis("Mouse X") * rotSpeed;
rot -= w;
}同理對于縱向旋轉我們需要設定一個范圍 所以代碼如下
//縱向旋轉角度
public float maxRoll = 70f * Mathf.PI * 2 / 360;
public float minRoll = 0f * Mathf.PI * 2 / 360;
//縱向旋轉速度
private float rollSpeed = 0.1f;
//縱向旋轉
public void Roll()
{
float w = Input.GetAxis("Mouse Y") * rollSpeed;
roll -= w;
if (roll > maxRoll)
roll = maxRoll;
if (roll < minRoll)
roll = minRoll;
}滾輪調節距離
通過鼠標滾輪調整相機與物體之間的距離
代碼如下
//距離范圍
public float maxDistance = 22f;
public float minDistance = 5f;
//距離變化速度
public float zoomSpeed = 0.2f;
//調整距離
public void Zoom()
{
if(Input.GetAxis("Mouse ScrollWheel") >0)
{
if (distance > minDistance)
distance -= zoomSpeed;
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (distance < maxDistance)
distance += zoomSpeed;
}
}全部代碼
public class CameraFollow : MonoBehaviour
{
//距離
public float distance = 15;
//橫向角度
public float rot = 0;
//縱向角度 30d度
public float roll = 30f * Mathf.PI * 2 / 360;
//目標物體
public GameObject target;
//橫向旋轉速度
public float rotSpeed=0.1f;
//縱向旋轉角度
public float maxRoll = 70f * Mathf.PI * 2 / 360;
public float minRoll = 0f * Mathf.PI * 2 / 360;
//縱向旋轉速度
private float rollSpeed = 0.1f;
//距離范圍
public float maxDistance = 22f;
public float minDistance = 5f;
//距離變化速度
public float zoomSpeed = 0.2f;
private void Start()
{
target = GameObject.Find("Black Track");
SetTarget(target);
}
private void LateUpdate()
{
if (target == null)
return;
if (Camera.main == null)
return;
//橫向旋轉
Rotate();
//縱向旋轉
Roll();
//縮放
Zoom();
//目標的坐標
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);
}
//設置目標
public void SetTarget(GameObject target)
{
if (target.transform.Find("cameraPoint") != null)
this.target = target.transform.Find("cameraPoint").gameObject;
else
this.target = target;
}
//橫向旋轉
public void Rotate()
{
float w = Input.GetAxis("Mouse X") * rotSpeed;
rot -= w;
}
//縱向旋轉
public void Roll()
{
float w = Input.GetAxis("Mouse Y") * rollSpeed;
roll -= w;
if (roll > maxRoll)
roll = maxRoll;
if (roll < minRoll)
roll = minRoll;
}
//調整距離
public void Zoom()
{
if(Input.GetAxis("Mouse ScrollWheel") >0)
{
if (distance > minDistance)
distance -= zoomSpeed;
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (distance < maxDistance)
distance += zoomSpeed;
}
}
}關于Unity3D實現相機跟隨控制的方法就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。