[英]Camera shaking on rotating player while crouching in unity
跟隨播放器的相機出現問題。 當播放器移動時,相機會晃動,並且當播放器處於蹲伏位置時,此效果會更加明顯。 我正在為混合動畫的播放器使用根運動。
播放器腳本:
Transform cameraT;
void Start () {
cameraT = Camera.main.transform;
}
void FixedUpdate ()
{
float sideMotion = Input.GetAxis("SideMotion");
float straightMotion= Input.GetAxis("StraightMotion");
if (Mathf.Abs(sideMotion) > 0 || Mathf.Abs(straightMotion) > 0)
{
transform.eulerAngles = new Vector3(transform.eulerAngles.x,
cameraT.eulerAngles.y);
}
}
相機腳本:
public float distanceFromPlayer=2f;
public float mouseSensitivity=6;
public Transform player;
public Vector2 pitchConstraint= new Vector2(-30,80);
Vector3 rotationSmoothVelocity;
Vector3 currentRotation;
public float rotationSmoothTime = 0.2f;
float yaw; //Rotation around the vertical axis is called yaw
float pitch; //Rotation around the side-to-side axis is called pitch
private void LateUpdate()
{
yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
pitch = Mathf.Clamp(pitch, pitchConstraint.x, pitchConstraint.y);
currentRotation = Vector3.SmoothDamp
(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
transform.eulerAngles = currentRotation;
transform.position = player.position - transform.forward * distanceFromPlayer;
}
對於公共轉換,我只是將一個空白的游戲對象添加到玩家的胸口級別並將其作為玩家的父元素。 我從一個在線教程中獲得了旋轉攝像機的流暢技巧,但是,盡管如此,確切的事情卻無法在玩家旋轉時起作用。
角色控制器連接到玩家,沒有任何剛體或對撞機。
您將要在相機上使用Lerp,以使其從一個位置移動到另一個位置變得平滑。 腳本API有一個很好的Vector3.Lerp示例。
因此,在您的情況下,您也可以添加一個公共變量以平滑位置。 類似於positionSmoothTime。 然后創建一個“所需位置”變量,我們將其稱為destPosition。
Vector3 destPosition = player.position - transform.forward * distanceFromPlayer;
transform.position = Vector3.Lerp(transform.position, destPosition, positionSmoothTime);
這樣可以有效地平滑到卡住的位置。 可以幫助其他人提到的另一件事是將身體移動較少的部分(例如頭部)用作目標。 結合使用Lerp功能,可以使相機平穩移動。
另一個大的幫助是將事物移到Update()函數中。 原因是, FixedUpdate()不會在每一幀都被調用。 因此,您可能會因此結結巴巴。 如果將所有內容移到Update()中 ,它將更新每一幀並有助於使內容平滑。 如果這樣做,則需要將所有運動乘以Time.deltaTime ,如下例所示。
Vector3 destPosition = (player.position - transform.forward * distanceFromPlayer) * Time.deltaTime;
有關更多示例,請查看每個函數的鏈接,以查看其上的Unity文檔。 它具有我在此處顯示的所有內容的示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.