Unity3D 學(xué)習(xí)筆記(四) 自由變換攝像頭視角

學(xué)習(xí)內(nèi)容:b站傅老師的U3D黑魂復(fù)刻課程

Unity3D 學(xué)習(xí)筆記 自由變換攝像頭視角

在角色下創(chuàng)建CameraHandle,將攝像機(jī)拖入

PlayerHandle控制水平旋轉(zhuǎn),CameraHandle控制豎直旋轉(zhuǎn)

定義

public PlayerController pi;

private GameObject playerHandle;

private GameObject cameraHandle;

獲取object

void Awake()

{

cameraHandle = transform.parent.gameObject;

playerHandle = cameraHandle.transform.parent.gameObject;

}

使用rotate方法旋轉(zhuǎn)

playerHandle.transform.Rotate(Vector3.up,pi.Jright * 10.0f *Time.deltaTime);

添加速度系數(shù)

public float horizontalSpeed = 50.0f;

兩個方向的旋轉(zhuǎn)方法調(diào)用

void Update()

{

playerHandle.transform.Rotate(Vector3.up,pi.Jright * horizontalSpeed *Time.deltaTime);

cameraHandle.transform.Rotate(Vector3.right,pi.Jup * -verticalSpeed *Time.deltaTime);

}

找到俯角仰角的臨界值,設(shè)定邊界

cameraHandle.transform.eulerAngles = new Vector3(

Mathf.Clamp(cameraHandle.transform.eulerAngles.x,-40,30),0,0);

但是這樣俯視可以,仰視會不斷鬼畜,注釋掉rotate,換一種方法

創(chuàng)建臨時變量

tempEulerX -= pi.Jup * verticalSpeed *Time.deltaTime;

tempEulerX = Mathf.Clamp(tempEulerX,-40,30);

cameraHandle.transform.localEulerAngles = new Vector3(

tempEulerX,0,0);

即可實(shí)現(xiàn)有邊界的視角范圍

鼠標(biāo)控制

如果想要使用鼠標(biāo)控制的話,可以加入鼠標(biāo)控制

Jright = Input.GetAxis("Mouse X");

Jup = Input.GetAxis("Mouse Y");

Unity3D 學(xué)習(xí)筆記 自由變換攝像頭視角

延遲鏡頭

使鏡頭出現(xiàn)遲滯跟隨,當(dāng)人物靜止時候停下來。

Unity3D 學(xué)習(xí)筆記 自由變換攝像頭視角

新建空物體camerapos

camera = Camera.main.gameObject;

獲取camera

camera = Camera.main.gameObject;

使camera追隨cameraPos

camera.transform.position = Vector3.SmoothDamp(camera.transform.position,transform.position,ref cameraDampVelocity,cameraDampValue);

camera.transform.eulerAngles = transform.eulerAngles;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容