Unity3D實(shí)現(xiàn)相機(jī)跟隨控制
本文實(shí)例為大家分享了Unity3D實(shí)現(xiàn)相機(jī)跟隨控制的具體代碼,供大家參考,具體內(nèi)容如下
跟隨算法
要實(shí)現(xiàn)3D攝像機(jī)的控制第一步就是先實(shí)現(xiàn)攝像機(jī)跟隨物體移動(dòng)。
要想讓相機(jī)跟隨物體移動(dòng),就要明白在一定角度下相機(jī)與物體的位置關(guān)系。
首先設(shè)置相機(jī)與物體之間的距離distance,相機(jī)與xz平面的角度為roll
所以根據(jù)三角關(guān)系可以求得映射在xz平面的距離d為distancecos(rool),相機(jī)高度為distancesin(roll)。
如下圖

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

所以說(shuō)開(kāi)始的時(shí)候指定distance和rot和roll就可以實(shí)現(xiàn)跟隨了。實(shí)現(xiàn)跟隨的代碼如下
public class CameraFollow : MonoBehaviour
{
//距離
public float distance = 15;
//橫向角度
public float rot = 0;
//縱向角度 30d度
public float roll = 30f * Mathf.PI * 2 / 360;
//目標(biāo)物體
public GameObject target;
private void Start()
{
target = GameObject.Find("Black Track");
}
private void LateUpdate()
{
if (target == null)
return;
if (Camera.main == null)
return;
//目標(biāo)的坐標(biāo)
Vector3 targetPos = target.transform.position;
//用三角函數(shù)計(jì)算相機(jī)的位置
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);
}
}
在跟隨的時(shí)候我們可以在要跟隨的物體下放置一個(gè)子物體命名為cameraPoint使相機(jī)對(duì)準(zhǔn)這個(gè)子物體從而方便的更改攝像機(jī)的視角。
所以在物體下添加一個(gè)cameraPoint的子物體

并且添加代碼
//設(shè)置目標(biāo)
public void SetTarget(GameObject target)
{
if (target.transform.Find("cameraPoint") != null)
this.target = target.transform.Find("cameraPoint").gameObject;
else
this.target = target;
}
如果準(zhǔn)的物體有名為cameraPoint的子物體,那么相機(jī)對(duì)準(zhǔn)cameraPoint子物體。
橫向與縱向旋轉(zhuǎn)攝像機(jī)
當(dāng)鼠標(biāo)向左移動(dòng)時(shí),相機(jī)隨之左轉(zhuǎn),當(dāng)鼠標(biāo)向右移動(dòng)時(shí),相機(jī)隨之右轉(zhuǎn)。
Unity的輸入軸Mouse X 和 Mouse Y 代表著鼠標(biāo)的移動(dòng)增量,也就是說(shuō)當(dāng)鼠標(biāo)向左移動(dòng)時(shí),Input.GetAxis(“Mouse X”)的值會(huì)增大,向右則減少。只要讓旋轉(zhuǎn)角度rot與Mouse X成正比關(guān)系,便能通過(guò)鼠標(biāo)控制攝像機(jī)的角度。
代碼如下
//橫向旋轉(zhuǎn)速度
public float rotSpeed=0.1f;
//橫向旋轉(zhuǎn)
public void Rotate()
{
float w = Input.GetAxis("Mouse X") * rotSpeed;
rot -= w;
}
同理對(duì)于縱向旋轉(zhuǎn)我們需要設(shè)定一個(gè)范圍 所以代碼如下
//縱向旋轉(zhuǎn)角度
public float maxRoll = 70f * Mathf.PI * 2 / 360;
public float minRoll = 0f * Mathf.PI * 2 / 360;
//縱向旋轉(zhuǎn)速度
private float rollSpeed = 0.1f;
//縱向旋轉(zhuǎn)
public void Roll()
{
float w = Input.GetAxis("Mouse Y") * rollSpeed;
roll -= w;
if (roll > maxRoll)
roll = maxRoll;
if (roll < minRoll)
roll = minRoll;
}
滾輪調(diào)節(jié)距離
通過(guò)鼠標(biāo)滾輪調(diào)整相機(jī)與物體之間的距離
代碼如下
//距離范圍
public float maxDistance = 22f;
public float minDistance = 5f;
//距離變化速度
public float zoomSpeed = 0.2f;
//調(diào)整距離
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;
//目標(biāo)物體
public GameObject target;
//橫向旋轉(zhuǎn)速度
public float rotSpeed=0.1f;
//縱向旋轉(zhuǎn)角度
public float maxRoll = 70f * Mathf.PI * 2 / 360;
public float minRoll = 0f * Mathf.PI * 2 / 360;
//縱向旋轉(zhuǎn)速度
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;
//橫向旋轉(zhuǎn)
Rotate();
//縱向旋轉(zhuǎn)
Roll();
//縮放
Zoom();
//目標(biāo)的坐標(biāo)
Vector3 targetPos = target.transform.position;
//用三角函數(shù)計(jì)算相機(jī)的位置
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);
}
//設(shè)置目標(biāo)
public void SetTarget(GameObject target)
{
if (target.transform.Find("cameraPoint") != null)
this.target = target.transform.Find("cameraPoint").gameObject;
else
this.target = target;
}
//橫向旋轉(zhuǎn)
public void Rotate()
{
float w = Input.GetAxis("Mouse X") * rotSpeed;
rot -= w;
}
//縱向旋轉(zhuǎn)
public void Roll()
{
float w = Input.GetAxis("Mouse Y") * rollSpeed;
roll -= w;
if (roll > maxRoll)
roll = maxRoll;
if (roll < minRoll)
roll = minRoll;
}
//調(diào)整距離
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;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Unity3D 計(jì)時(shí)器的實(shí)現(xiàn)代碼(三種寫(xiě)法總結(jié))
- Unity3D UI Text得分?jǐn)?shù)字增加的實(shí)例代碼
- Unity3D實(shí)現(xiàn)模型隨機(jī)切割
- Unity3D實(shí)現(xiàn)物體排成弧行
- Unity3D生成一段隧道網(wǎng)格的方法
- unity3D實(shí)現(xiàn)物體任意角度自旋轉(zhuǎn)
- Unity3D實(shí)現(xiàn)自動(dòng)尋路
- Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲(2)
- Unity3d 使用Gizmos畫(huà)一個(gè)圓圈
相關(guān)文章
C#使用async和await實(shí)現(xiàn)異步編程
本文詳細(xì)講解了C#使用async和await實(shí)現(xiàn)異步編程的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
C#使用DropDownList綁定添加新數(shù)據(jù)的方法匯總
這篇文章主要介紹了C#使用DropDownList綁定添加新數(shù)據(jù)的方法匯總的相關(guān)資料,需要的朋友可以參考下2016-03-03
總結(jié)C#動(dòng)態(tài)調(diào)用WCF接口的兩種方法
這篇文章給大家總結(jié)了C#動(dòng)態(tài)調(diào)用WCF接口的兩種方法,大家可以根據(jù)自己的需求選擇對(duì)應(yīng)的方式,下面來(lái)一起看看。2016-09-09
C#滑動(dòng)驗(yàn)證碼拼圖驗(yàn)證功能實(shí)現(xiàn)(SlideCaptcha)
目前網(wǎng)站上的驗(yàn)證碼機(jī)制可謂是五花八門(mén),有簡(jiǎn)單的數(shù)字驗(yàn)證,有摻雜了字母和文字的混淆驗(yàn)證,還有通過(guò)滑塊進(jìn)行的拼圖驗(yàn)證,下面這篇文章主要給大家介紹了關(guān)于C#滑動(dòng)驗(yàn)證碼拼圖驗(yàn)證功能的實(shí)現(xiàn)方法,需要的朋友可以參考下2022-04-04
C#反射在實(shí)際應(yīng)用中的實(shí)例代碼
C#反射在實(shí)際應(yīng)用中的實(shí)例代碼,需要的朋友可以參考一下2013-03-03
C#查詢(xún)SqlServer數(shù)據(jù)庫(kù)并返回單個(gè)值的方法
這篇文章主要介紹了C#查詢(xún)SqlServer數(shù)據(jù)庫(kù)并返回單個(gè)值的方法,涉及C#操作SQLServer數(shù)據(jù)庫(kù)查詢(xún)的相關(guān)技巧,需要的朋友可以參考下2015-06-06
C#圖像處理之圖像目標(biāo)質(zhì)心檢測(cè)的方法
這篇文章主要介紹了C#圖像處理之圖像目標(biāo)質(zhì)心檢測(cè)的方法,可實(shí)現(xiàn)C#計(jì)算圖像質(zhì)心的相關(guān)技巧,需要的朋友可以參考下2015-04-04
c#制作簡(jiǎn)單啟動(dòng)畫(huà)面的方法
這篇文章主要介紹了c#制作簡(jiǎn)單啟動(dòng)畫(huà)面的方法,涉及C#實(shí)現(xiàn)桌面程序啟動(dòng)畫(huà)面的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04

