實(shí)現(xiàn)WSAD移動(dòng)方向,鼠標(biāo)右鍵旋轉(zhuǎn)角度,滾輪實(shí)現(xiàn)縮放
在此之前,我們先講清楚幾個(gè)比較容易混淆的點(diǎn):
1:Transform.forward是Transform組件的一個(gè)Vector3類(lèi)型的屬性,代表游戲?qū)ο笤谑澜缱鴺?biāo)系中的朝向,即“前方向”,它的值是(0,0,1),它表示了一個(gè)對(duì)象的X軸方向,一般用于計(jì)算游戲?qū)ο笄斑M(jìn)的方向,比如用于移動(dòng)、旋轉(zhuǎn)和射線檢測(cè)等場(chǎng)景。
2:Transform.Right的數(shù)學(xué)表達(dá)式是(1, 0, 0)。它表示了一個(gè)對(duì)象的X軸方向,或者說(shuō)是它的右側(cè)方向
值得注意的是,它們都是單位向量,一般只用于方向計(jì)算
3:Input.GetAxis("Horizontal")和Input.GetAxis("Vertical"),input.GetAxis("Mouse X")
它們是分別獲取水平方向輸入和豎直方向輸入的函數(shù),這個(gè)函數(shù)會(huì)返回一個(gè)范圍在-1到1之間的浮點(diǎn)數(shù),表示當(dāng)前水平方向上的輸入狀態(tài)
比如Input.GetAxis("Mouse X")
返回鼠標(biāo)在水平方向上的移動(dòng),以浮點(diǎn)數(shù)表示。當(dāng)鼠標(biāo)向右移動(dòng)時(shí),返回正值;當(dāng)鼠標(biāo)向左移動(dòng)時(shí),返回負(fù)值。如果鼠標(biāo)不動(dòng),返回0
Input.GetAxis("Mouse ScrollWheel"),向前滾動(dòng)是正數(shù),向后滾動(dòng)時(shí)負(fù)數(shù)
4:歐拉角和四元數(shù)
在unity中的rotation就是使用了歐拉角,歐拉角指的是以三個(gè)軸為基準(zhǔn),通過(guò)繞不同軸的旋轉(zhuǎn)角度來(lái)表示一個(gè)物體的旋轉(zhuǎn)狀態(tài),通常有XYZ、ZYX、ZXY等多種旋轉(zhuǎn)順序。但是,歐拉角會(huì)存在萬(wàn)向鎖問(wèn)題,即在某些特定情況下,兩個(gè)軸的旋轉(zhuǎn)會(huì)發(fā)生重合,導(dǎo)致旋轉(zhuǎn)計(jì)算異常
四元數(shù)則是通過(guò)四元數(shù)的定義和運(yùn)算來(lái)實(shí)現(xiàn)旋轉(zhuǎn),四元數(shù)實(shí)際上是一個(gè)四元組,包含了一個(gè)實(shí)部和三個(gè)虛部。它們的定義方式和歐拉角不同,可以避免萬(wàn)向鎖問(wèn)題,并且旋轉(zhuǎn)順序不會(huì)影響旋轉(zhuǎn)結(jié)果。四元數(shù)還可以通過(guò)插值運(yùn)算實(shí)現(xiàn)平滑旋轉(zhuǎn),適用于需要連續(xù)旋轉(zhuǎn)的情況。
我把代碼分成了三塊
第一個(gè)模塊是WSAD控制前后左右,這段比較簡(jiǎn)單,直接定義一個(gè)V3類(lèi)型的移動(dòng)方向,再使用Translate即可
第二個(gè)模塊是使用QE控制上下視角,定義一個(gè)變量upDown,每次按鍵時(shí)候改變1,然后直接把位置賦值到相機(jī)位置即可
第三個(gè)模塊是使用鼠標(biāo)右鍵進(jìn)行旋轉(zhuǎn),設(shè)置兩個(gè)旋轉(zhuǎn)向量rotateX,rotateY,Y方向旋轉(zhuǎn)限值minAngle和maxAngle,然后把向量和速度加到相機(jī)的歐拉角即可
有一個(gè)難點(diǎn),在剛開(kāi)始調(diào)試的時(shí)候,第一次點(diǎn)擊鼠標(biāo)右鍵進(jìn)行旋轉(zhuǎn)時(shí),屏幕一直跳動(dòng),最后經(jīng)過(guò)GPT的幫助,最終解決了問(wèn)題!愛(ài)死GPT文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-486377.html
using UnityEngine;
public class CameraController : MonoBehaviour
{
//WSAD相機(jī)移動(dòng)速度
public float moveSpeed = 1.0f;
//相機(jī)當(dāng)前移動(dòng)方向
private Vector3 moveDirection;
//QE相機(jī)的目標(biāo)高度
private float upDistance;
public float upSpeed = 0.2f;
//鼠標(biāo)右鍵控制旋轉(zhuǎn)
private float rotateX,rotateY;
public float sensitivity =0.5f;
//控制鼠標(biāo)在Y方向上的限值
public float minAngle = -90f;
public float maxAngle = 90f;
//記錄之前的歐拉角,避免跳屏
private Vector3 currentRotation,lastPosition;
//鼠標(biāo)滾輪控制縮放
public float zoomSpeed = 10f;
private float zoomDistance = 0f;
private void Start()
{
currentRotation = transform.eulerAngles;
}
void Update()
{
// 使用WSAD控制相機(jī)前后左右移動(dòng)比較簡(jiǎn)單,直接獲取當(dāng)前的移動(dòng)方向,然后使用Translate移動(dòng)即可
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.Translate(moveDirection * moveSpeed * Time.deltaTime);
}
//使用QE控制相機(jī)的上下,使用unDown參數(shù)進(jìn)行單位變化
{
if (Input.GetKey(KeyCode.Q))
{
upDistance -= upSpeed * Time.deltaTime;
transform.Translate(transform.up * upDistance, Space.World);
}
else if(Input.GetKeyUp(KeyCode.Q))
{
upDistance = 0;
}
if (Input.GetKey(KeyCode.E))
{
upDistance += upSpeed * Time.deltaTime;
transform.Translate(transform.up * upDistance, Space.World);
}
else if(Input.GetKeyUp(KeyCode.E))
{
upDistance = 0;
}
}
//使用鼠標(biāo)右鍵來(lái)控制相機(jī)旋轉(zhuǎn)
//Mouse X,向右移動(dòng)返回正值,Mouse Y,向上移動(dòng)鼠標(biāo)為正值
{
if (Input.GetMouseButtonDown(1))
{
// 記錄當(dāng)前的歐拉角
//currentRotation = transform.eulerAngles;
//記錄鼠標(biāo)位置
lastPosition = Input.mousePosition;
}
if (Input.GetMouseButton(1))
{
//設(shè)置偏移量
Vector3 offset = Input.mousePosition - lastPosition;
rotateX += offset.x * sensitivity;
rotateY -= offset.y * sensitivity;
//給Y方向的旋轉(zhuǎn)加上限值函數(shù)
rotateY = Mathf.Clamp(rotateY, minAngle, maxAngle);
//改變當(dāng)前的歐拉角
transform.eulerAngles = new Vector3(rotateY, rotateX, 0f);
// 將保存的歐拉角重新賦值回去
transform.eulerAngles += currentRotation;
lastPosition = Input.mousePosition;
}
}
//使用滾輪來(lái)控制物體的縮放
{
//獲取滾輪的滾動(dòng)幅度和方向
zoomDistance += Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
//使用限值函數(shù)來(lái)限定縮放的范圍
zoomDistance = Mathf.Clamp(zoomDistance, -10f, 10f);
//最后定位
transform.position = transform.position + transform.forward * Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
}
}
}
直接將代碼復(fù)制,掛到Camera即可運(yùn)行!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-486377.html
到了這里,關(guān)于Unity相機(jī)自由移動(dòng)腳本的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!