From a85ae374365ab72e622f33dbf63733d2efdf1f58 Mon Sep 17 00:00:00 2001 From: Darion Spaargaren Date: Tue, 21 Jun 2022 12:27:49 +0200 Subject: [PATCH] Add linear interpolation to camera tilt --- scenes/warehouse.tscn | 1 + scripts/PlayerControllerKinematic.cs | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/scenes/warehouse.tscn b/scenes/warehouse.tscn index cb4f105..88dbd4e 100644 --- a/scenes/warehouse.tscn +++ b/scenes/warehouse.tscn @@ -20,6 +20,7 @@ shadow_enabled = true [node name="Player" parent="." instance=ExtResource( 6 )] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0 ) +CameraRollSpeed = 0.1 [node name="ConcreteFloor1" parent="." instance=ExtResource( 8 )] transform = Transform( 50, 0, 0, 0, 1, 0, 0, 0, 50, 0, 0, 0 ) diff --git a/scripts/PlayerControllerKinematic.cs b/scripts/PlayerControllerKinematic.cs index 4728b05..0e14eb5 100644 --- a/scripts/PlayerControllerKinematic.cs +++ b/scripts/PlayerControllerKinematic.cs @@ -143,17 +143,21 @@ private void HandleMovementInput(float delta) { inputDirection += Transform.basis.x; - cameraRotationDegrees.z = Mathf.Clamp(cameraRotationDegrees.z - CameraRollSpeed * maxMovementSpeed, -CameraRollMultiplier * maxMovementSpeed, CameraRollMultiplier * maxMovementSpeed); + cameraRotationDegrees.z = Mathf.Lerp(cameraRotationDegrees.z, Mathf.Clamp(cameraRotationDegrees.z - CameraRollSpeed * maxMovementSpeed, -CameraRollMultiplier * maxMovementSpeed, CameraRollMultiplier * maxMovementSpeed), CameraRollSpeed); } if (Input.IsActionPressed("move_left")) { inputDirection -= Transform.basis.x; - cameraRotationDegrees.z = Mathf.Clamp(cameraRotationDegrees.z + CameraRollSpeed * maxMovementSpeed, -CameraRollMultiplier * maxMovementSpeed, CameraRollMultiplier * maxMovementSpeed); + cameraRotationDegrees.z = Mathf.Lerp(cameraRotationDegrees.z, Mathf.Clamp(cameraRotationDegrees.z + CameraRollSpeed * maxMovementSpeed, -CameraRollMultiplier * maxMovementSpeed, CameraRollMultiplier * maxMovementSpeed), CameraRollSpeed); + } + if (!Input.IsActionPressed("move_right") && !Input.IsActionPressed("move_left")) + { + cameraRotationDegrees.z = Mathf.Lerp(camera.RotationDegrees.z, 0f, CameraRollSpeed); } - inputDirection = inputDirection.Normalized(); - camera.RotationDegrees = cameraRotationDegrees; + camera.RotationDegrees = cameraRotationDegrees; + inputDirection = inputDirection.Normalized(); targetVelocity += inputDirection * maxMovementSpeed; float decceleration = IsGrounded() ? Decceleration : 0.5f; @@ -225,10 +229,10 @@ private void ApplyVelocity(float delta) animationTree.Set("parameters/time_scale/scale", Mathf.Clamp(time_scale + delta, 0f, 1f)); } - if (GetLocalVelocity().Abs().x < 1f) - { - camera.RotationDegrees = new Vector3(camera.RotationDegrees.x, camera.RotationDegrees.y, 0f); - } + // if (GetLocalVelocity().Abs().x < 1f) + // { + // camera.RotationDegrees = new Vector3(camera.RotationDegrees.x, camera.RotationDegrees.y, Mathf.Lerp(camera.RotationDegrees.z, 0f, CameraRollSpeed)); + // } } } }