From 8c03b978a85f2a1e5be111184c553fadd6d1806a Mon Sep 17 00:00:00 2001 From: 0ceanSlim <89587889+0ceanSlim@users.noreply.github.com> Date: Fri, 3 Nov 2023 12:51:11 -0400 Subject: [PATCH] tried to add leaning --- Assets/Scripts/FirstPersonController.cs | 53 ++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/FirstPersonController.cs b/Assets/Scripts/FirstPersonController.cs index 3cd59062..8617c91c 100644 --- a/Assets/Scripts/FirstPersonController.cs +++ b/Assets/Scripts/FirstPersonController.cs @@ -19,6 +19,8 @@ public class FirstPersonController : MonoBehaviour [SerializeField] private bool canJump = true; [SerializeField] private bool canCrouch = true; [SerializeField] private bool canUseHeadbob = true; + [SerializeField] private bool canLean = true; + [Header("Controls")] [SerializeField] private KeyCode sprintKey = KeyCode.LeftShift; @@ -43,6 +45,17 @@ public class FirstPersonController : MonoBehaviour [SerializeField, Range(1, 180)] private float upperLookLimit = 80.0f; [SerializeField, Range(1, 180)] private float lowerLookLimit = 80.0f; + + [Header("Leaning Parameters")] + [SerializeField] private bool toggleLeaning = false; // Toggle mode + [SerializeField] private KeyCode leanLeftKey = KeyCode.Q; + [SerializeField] private KeyCode leanRightKey = KeyCode.E; + [SerializeField, Range(1, 45)] private float maxLeaningAngle = 15.0f; + private float currentLeaningAngle; + private bool isLeaningLeft; + private bool isLeaningRight; + + [Header("Jumping Parameters")] [SerializeField] private float jumpForce = 8.0f; [SerializeField] private float gravity = 30.0f; @@ -122,6 +135,9 @@ public class FirstPersonController : MonoBehaviour if (canUseHeadbob) HandleHeadbob(); + if (canLean) + HandleLeaning(); // Added the HandleLeaning method call here. + ApplyFinalMovements(); } } @@ -236,10 +252,45 @@ public class FirstPersonController : MonoBehaviour { rotationX -= Input.GetAxis("Mouse Y") * lookSpeedY; rotationX = Mathf.Clamp(rotationX, -upperLookLimit, lowerLookLimit); - playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0); + + // Apply the current leaning angle to the camera's rotation + playerCamera.transform.localRotation = Quaternion.Euler(rotationX + currentLeaningAngle, 0, 0); transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeedX, 0); } + private void HandleLeaning() + { + if (toggleLeaning) + { + if (Input.GetKeyDown(leanLeftKey)) + { + isLeaningLeft = !isLeaningLeft; + isLeaningRight = false; + } + if (Input.GetKeyDown(leanRightKey)) + { + isLeaningRight = !isLeaningRight; + isLeaningLeft = false; + } + } + else + { + if (Input.GetKey(leanLeftKey) && !isLeaningRight) + { + isLeaningLeft = true; + isLeaningRight = false; + } + if (Input.GetKey(leanRightKey) && !isLeaningLeft) + { + isLeaningRight = true; + isLeaningLeft = false; + } + } + + // Apply the current leaning angle using the maximum leaning angle + currentLeaningAngle = Mathf.Clamp(currentLeaningAngle, -maxLeaningAngle, maxLeaningAngle); + } + private void ApplyFinalMovements() { if (!characterController.isGrounded)