tried to add leaning

This commit is contained in:
0ceanSlim 2023-11-03 12:51:11 -04:00
parent fb6157d752
commit 8c03b978a8

View File

@ -19,6 +19,8 @@ public class FirstPersonController : MonoBehaviour
[SerializeField] private bool canJump = true; [SerializeField] private bool canJump = true;
[SerializeField] private bool canCrouch = true; [SerializeField] private bool canCrouch = true;
[SerializeField] private bool canUseHeadbob = true; [SerializeField] private bool canUseHeadbob = true;
[SerializeField] private bool canLean = true;
[Header("Controls")] [Header("Controls")]
[SerializeField] private KeyCode sprintKey = KeyCode.LeftShift; [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 upperLookLimit = 80.0f;
[SerializeField, Range(1, 180)] private float lowerLookLimit = 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")] [Header("Jumping Parameters")]
[SerializeField] private float jumpForce = 8.0f; [SerializeField] private float jumpForce = 8.0f;
[SerializeField] private float gravity = 30.0f; [SerializeField] private float gravity = 30.0f;
@ -122,6 +135,9 @@ public class FirstPersonController : MonoBehaviour
if (canUseHeadbob) if (canUseHeadbob)
HandleHeadbob(); HandleHeadbob();
if (canLean)
HandleLeaning(); // Added the HandleLeaning method call here.
ApplyFinalMovements(); ApplyFinalMovements();
} }
} }
@ -236,10 +252,45 @@ public class FirstPersonController : MonoBehaviour
{ {
rotationX -= Input.GetAxis("Mouse Y") * lookSpeedY; rotationX -= Input.GetAxis("Mouse Y") * lookSpeedY;
rotationX = Mathf.Clamp(rotationX, -upperLookLimit, lowerLookLimit); 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); 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() private void ApplyFinalMovements()
{ {
if (!characterController.isGrounded) if (!characterController.isGrounded)