我有第三人称密码,但是当我向后走时,相机会停顿!有没有人有办法解决吗?相机与播放器是分开的,播放器根据相机的视角旋转。
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);
}
答案 0 :(得分:2)
以我的经验,当您有一台光滑的相机结结巴巴时,这是因为相机-达到速度后-移动速度快于其跟随的物体:
简单; 调整摄像头速度变量,以确保对象移动时永远不会完全赶上,而是停留在后面,因此不必每隔几帧就停下来。
其他来源经常会解释他们的解决方案是不正确将代码放入FixedUpdate中,以奇迹般地摆脱断断续续的行为。有时通过以下操作获得正确的行为是由于一系列原因:
float step = CameraMoveSpeed * Time.deltaTime;
为了简单的数学,假设CameraMoveSpeed为1。通过这些数字,我们可以理解将代码放入Update会得到以下结果
更新
每0.007秒,我们使用CameraMoveSpeed * 0.007更新相机lerp刻度
FixedUpdate
每0.02秒,我们会使用CameraMoveSpeed * 0.007更新相机lerp刻度
结果
它们在每个刻度上移动相同的长度,但是在FixedUpdate中,刻度的频率不高,这会导致相机平滑速度变慢,因此“解决”了此问题的描述,以某种方式调节相机速度变量以使摄像机的速度变慢,但是您没有调整变量,而是将代码移动到了物理更新而不是帧更新,并且您只是偶然解决了此问题,而不了解原因。