本章代碼基于前兩章。
1. 我們新建CameraController腳本,將其掛載到Camera上
2. 在角色Player下新建一個(gè)空物體,命名為cameraTargetPoint,并將該物體掛載至CameraController腳本中【注意代碼中的這行:public Transform cameraTargetPoint;】,將該空物體放在人物頭部附近位置
3.將PlayerController腳本綁定至CameraController中【public PlayerController playerController;】
4.本章代碼的實(shí)現(xiàn)原理是讓攝像機(jī)對(duì)準(zhǔn)空物件,并保持一定距離
5. 具體代碼如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
private static float lower_upper_bound = 60f;
public Transform cameraTargetPoint;
public float cameraDistance = 5.0f;
public Vector2 verticalRange = new Vector2(-lower_upper_bound, lower_upper_bound);
public float cameraSensitivity = 120f;
private Transform verticalObj;
private Transform horizontalObj;
private float horizontalValue;
private float verticalValue;
public PlayerController playerController;
// Start is called before the first frame update
void Start()
{
horizontalObj = new GameObject("Camera Horizontal").transform;
horizontalObj.SetParent(cameraTargetPoint);
horizontalObj.localPosition = Vector3.zero;
horizontalObj.localRotation = Quaternion.identity;
verticalObj = new GameObject("Camera Vertical").transform;
verticalObj.SetParent(horizontalObj);
verticalObj.localPosition = Vector3.zero;
verticalObj.localRotation = Quaternion.identity;
transform.SetParent(verticalObj);
transform.localPosition = new Vector3(0,0,-cameraDistance);
transform.localRotation = Quaternion.identity;
}
// Update is called once per frame
void Update()
{
cameraMoving();
DoNotTouchTheGround();
}
private void cameraMoving()
{
horizontalValue += Input.GetAxis("Mouse X") * Time.deltaTime * cameraSensitivity;
horizontalObj.localRotation = Quaternion.Euler(new Vector3(0, horizontalValue, 0));
verticalValue += Input.GetAxis("Mouse Y") * Time.deltaTime * cameraSensitivity;
verticalValue = Mathf.Clamp(verticalValue, verticalRange.x, verticalRange.y);
verticalObj.localRotation = Quaternion.Euler(new Vector3(-verticalValue, 0, 0));
}
private void DoNotTouchTheGround() // 攝像機(jī)與角色點(diǎn)中間有物體時(shí),讓攝像機(jī)處于物體前,避免遮擋
{
Vector3 distanceVec = transform.position - cameraTargetPoint.position;
float distance = distanceVec.magnitude;
RaycastHit hit;
if(Physics.Raycast(cameraTargetPoint.position,distanceVec, out hit,distance))
{
if(hit.transform.tag != "Player")
{
transform.position = hit.point;
}
else
{
transform.localPosition = Vector3.Lerp(transform.localPosition, new Vector3(0, 0, -distance), 0.3f);
}
}
else
{
transform.localPosition = Vector3.Lerp(transform.localPosition, new Vector3(0, 0, -distance), 0.3f);
}
}
}
Tips:以上代碼中,DoNotTouchTheGround()方法不是必須的,該方法是用于限制當(dāng)攝像機(jī)與角色之間有障礙物時(shí),把攝像機(jī)放在障礙物前方以避免視覺阻礙,可以看情況添加。
至此,我們可以實(shí)現(xiàn)攝像機(jī)跟隨角色移動(dòng),并且鼠標(biāo)能操控?cái)z像機(jī)繞著角色轉(zhuǎn)動(dòng),但仍有一個(gè)問題:當(dāng)攝像機(jī)位于人物前方時(shí),人物前進(jìn)方向仍是我們的后方,這是由于參考坐標(biāo)系的緣故,具體修復(fù)如下:
1. 在PlayerController腳本中,添加
public Transform cameraTargetPoint;
在Move方法中添加【moveingVec = cameraTargetPoint.TransformDirection(moveingVec);】:
private void Move()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 moveingVec = new Vector3(horizontalInput, 0f, verticalInput) * Time.deltaTime * speed;
moveingVec = cameraTargetPoint.TransformDirection(moveingVec);
transform.Translate(moveingVec);
}
2. 在CameraController腳本中的cameraMoving()方法中添加下句:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-835167.html
playerController.cameraTargetPoint = horizontalObj;
于是即可實(shí)現(xiàn)攝像頭跟隨鼠標(biāo),并圍繞角色運(yùn)動(dòng),并且角色運(yùn)動(dòng)方向【僅僅是方向,如果要人物轉(zhuǎn)向,則還需其他修改,本系列其他章節(jié)會(huì)有涉及】可以自適應(yīng)為當(dāng)前攝像頭的前方文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-835167.html
到了這里,關(guān)于Unity【角色/攝像機(jī)移動(dòng)控制】【3.攝像機(jī)跟隨角色】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!