当我向后走时,第三人称视角的相机卡顿
Third-person camera stutters when I walk backwards
我有第三人称代码,但是我倒着走的时候镜头卡顿了!有没有人有办法解决吗?摄像机与播放器分离,播放器根据摄像机视角旋转。
PlayerController.cs
if (Input.GetKey (KeyCode.LeftShift)) {
if(Input.GetKey(KeyCode.W) || Input.GetAxis ("Vertical") > 0) moveDirection += camera.forward;
if(Input.GetKey(KeyCode.S) || Input.GetAxis ("Vertical") < 0) moveDirection += -camera.forward;
if(Input.GetKey(KeyCode.A) || Input.GetAxis ("Horizontal") < 0) moveDirection += -camera.right;
if(Input.GetKey(KeyCode.D) || Input.GetAxis ("Horizontal") > 0) moveDirection += camera.right;
if (Input.GetAxis ("Horizontal") != 0 || Input.GetAxis ("Vertical") != 0) {
//Multiply it by speed.
moveDirection.Normalize ();
moveDirection *= run;
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move (moveDirection * Time.deltaTime);
}
moveDirection.y = 0f;
if (moveDirection != Vector3.zero) {
transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (moveDirection), rotationSpeed * Time.deltaTime);
}
}
Camera.cs
// Update is called once per frame
void Update () {
// We setup the rotation of the sticks here
float inputX = Input.GetAxis ("RightStickHorizontal");
float inputZ = Input.GetAxis ("RightStickVertical");
mouseX = Input.GetAxis ("Mouse X");
mouseY = Input.GetAxis ("Mouse Y");
finalInputX = inputX + mouseX;
finalInputZ = inputZ - mouseY;
rotY += finalInputX * inputSensitivity * Time.deltaTime;
rotX += finalInputZ * inputSensitivity * Time.deltaTime;
rotX = Mathf.Clamp (rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler (rotX, rotY, 0.0f);
transform.rotation = localRotation;
void LateUpdate () {
CameraUpdater ();
}
void CameraUpdater() {
Transform target = CameraFollowObj.transform;
// set the target object to follow
//move towards the game object that is the target
float step = CameraMoveSpeed * Time.fixedDeltaTime;
transform.position = Vector3.MoveTowards (transform.position, CameraFollowObj.transform.position, step);
}
根据我的经验,如果你的相机流畅但会卡顿,那是因为相机 - 一旦达到速度 - 移动速度比它跟随的物体快:
问题
- 对象移动
- 相机开始慢慢移向物体
- 它追上来停下来
- 相机开始慢慢移向物体
- 重复2-4;口吃行为
解决方案
简单; 调整相机速度变量以确保它永远不会完全在物体移动时赶上,而是停留在后面所以它没有每隔几帧停止一次。
阅读奖励material
其他消息来源通常会解释他们的解决方案是错误地将代码放入 FixedUpdate 以奇迹般地摆脱卡顿行为。有时从这样做得到正确的行为是由于一系列的事情:
- Update 运行每帧更新,所以这里的时间步长 (Time.deltaTime) 取决于机器,但在我的机器上,平均每 0.007 秒。
- FixedUpdate 在固定的时间步长上运行,标准是 0.002,但可以在设置中更改。
- 代码:
float step = CameraMoveSpeed * Time.deltaTime;
为了简单的数学计算,我们假设 CameraMoveSpeed 为 1。
因此,根据这些数字,我们可以理解将代码放入 Update 会产生以下结果
更新
每 0.007 秒,我们用 CameraMoveSpeed * 0.007
更新相机 lerp tick
固定更新
每 0.02 秒,我们用 CameraMoveSpeed * 0.007
更新相机 lerp tick
结果
它们每次移动的长度相同,但固定更新中的更新频率较低,这导致相机平滑速度较慢,因此,"solves" 问题被描述为对此 post 的回答
我有第三人称代码,但是我倒着走的时候镜头卡顿了!有没有人有办法解决吗?摄像机与播放器分离,播放器根据摄像机视角旋转。
PlayerController.cs
if (Input.GetKey (KeyCode.LeftShift)) {
if(Input.GetKey(KeyCode.W) || Input.GetAxis ("Vertical") > 0) moveDirection += camera.forward;
if(Input.GetKey(KeyCode.S) || Input.GetAxis ("Vertical") < 0) moveDirection += -camera.forward;
if(Input.GetKey(KeyCode.A) || Input.GetAxis ("Horizontal") < 0) moveDirection += -camera.right;
if(Input.GetKey(KeyCode.D) || Input.GetAxis ("Horizontal") > 0) moveDirection += camera.right;
if (Input.GetAxis ("Horizontal") != 0 || Input.GetAxis ("Vertical") != 0) {
//Multiply it by speed.
moveDirection.Normalize ();
moveDirection *= run;
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move (moveDirection * Time.deltaTime);
}
moveDirection.y = 0f;
if (moveDirection != Vector3.zero) {
transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (moveDirection), rotationSpeed * Time.deltaTime);
}
}
Camera.cs
// Update is called once per frame
void Update () {
// We setup the rotation of the sticks here
float inputX = Input.GetAxis ("RightStickHorizontal");
float inputZ = Input.GetAxis ("RightStickVertical");
mouseX = Input.GetAxis ("Mouse X");
mouseY = Input.GetAxis ("Mouse Y");
finalInputX = inputX + mouseX;
finalInputZ = inputZ - mouseY;
rotY += finalInputX * inputSensitivity * Time.deltaTime;
rotX += finalInputZ * inputSensitivity * Time.deltaTime;
rotX = Mathf.Clamp (rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler (rotX, rotY, 0.0f);
transform.rotation = localRotation;
void LateUpdate () {
CameraUpdater ();
}
void CameraUpdater() {
Transform target = CameraFollowObj.transform;
// set the target object to follow
//move towards the game object that is the target
float step = CameraMoveSpeed * Time.fixedDeltaTime;
transform.position = Vector3.MoveTowards (transform.position, CameraFollowObj.transform.position, step);
}
根据我的经验,如果你的相机流畅但会卡顿,那是因为相机 - 一旦达到速度 - 移动速度比它跟随的物体快:
问题
- 对象移动
- 相机开始慢慢移向物体
- 它追上来停下来
- 相机开始慢慢移向物体
- 重复2-4;口吃行为
解决方案
简单; 调整相机速度变量以确保它永远不会完全在物体移动时赶上,而是停留在后面所以它没有每隔几帧停止一次。
阅读奖励material
其他消息来源通常会解释他们的解决方案是错误地将代码放入 FixedUpdate 以奇迹般地摆脱卡顿行为。有时从这样做得到正确的行为是由于一系列的事情:
- Update 运行每帧更新,所以这里的时间步长 (Time.deltaTime) 取决于机器,但在我的机器上,平均每 0.007 秒。
- FixedUpdate 在固定的时间步长上运行,标准是 0.002,但可以在设置中更改。
- 代码:
float step = CameraMoveSpeed * Time.deltaTime;
为了简单的数学计算,我们假设 CameraMoveSpeed 为 1。
因此,根据这些数字,我们可以理解将代码放入 Update 会产生以下结果
更新
每 0.007 秒,我们用 CameraMoveSpeed * 0.007
更新相机 lerp tick固定更新
每 0.02 秒,我们用 CameraMoveSpeed * 0.007
更新相机 lerp tick结果
它们每次移动的长度相同,但固定更新中的更新频率较低,这导致相机平滑速度较慢,因此,"solves" 问题被描述为对此 post 的回答