fps controller changes

This commit is contained in:
Chris kerr 2024-08-27 20:38:21 -04:00
parent 1931283255
commit 644bce2a17

View File

@ -1,14 +1,13 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using SimpleInventorySystem; using SimpleInventorySystem;
public class FirstPersonController : MonoBehaviour public class FirstPersonController : MonoBehaviour
{ {
public InventorySystem inventorySystem; // Reference to the InventorySystem script public InventorySystem inventorySystem; // Reference to the InventorySystem script
// Start is called before the first frame update // Flags for various character states and actions
public bool CanMove { get; private set; } = true; public bool CanMove { get; private set; } = true;
private bool IsSprinting => canSprint && Input.GetKey(sprintKey); private bool IsSprinting => canSprint && Input.GetKey(sprintKey);
private bool ShouldJump => Input.GetKeyDown(jumpKey) && characterController.isGrounded; private bool ShouldJump => Input.GetKeyDown(jumpKey) && characterController.isGrounded;
@ -21,7 +20,6 @@ public class FirstPersonController : MonoBehaviour
[SerializeField] private bool canUseHeadbob = true; [SerializeField] private bool canUseHeadbob = true;
[SerializeField] private bool canLean = true; [SerializeField] private bool canLean = true;
[Header("Controls")] [Header("Controls")]
[SerializeField] private KeyCode sprintKey = KeyCode.LeftShift; [SerializeField] private KeyCode sprintKey = KeyCode.LeftShift;
[SerializeField] private KeyCode jumpKey = KeyCode.Space; [SerializeField] private KeyCode jumpKey = KeyCode.Space;
@ -33,19 +31,16 @@ public class FirstPersonController : MonoBehaviour
[SerializeField] private float walkSpeedInterval = 0.5f; [SerializeField] private float walkSpeedInterval = 0.5f;
private float currentWalkSpeed; private float currentWalkSpeed;
[SerializeField] private float walkSpeed = 3.0f; [SerializeField] private float walkSpeed = 3.0f;
[SerializeField] private float sprintSpeed = 6.0f; [SerializeField] private float sprintSpeed = 6.0f;
[SerializeField] private float crouchSpeed = 1.5f; [SerializeField] private float crouchSpeed = 1.5f;
[Header("Look Parameters")] [Header("Look Parameters")]
[SerializeField, Range(1, 10)] private float lookSpeedX = 2.0f; [SerializeField, Range(1, 10)] private float lookSpeedX = 2.0f;
[SerializeField, Range(1, 10)] private float lookSpeedY = 2.0f; [SerializeField, Range(1, 10)] private float lookSpeedY = 2.0f;
[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")] [Header("Leaning Parameters")]
[SerializeField] private bool toggleLeaning = false; // Toggle mode [SerializeField] private bool toggleLeaning = false; // Toggle mode
[SerializeField] private KeyCode leanLeftKey = KeyCode.Q; [SerializeField] private KeyCode leanLeftKey = KeyCode.Q;
@ -55,7 +50,6 @@ public class FirstPersonController : MonoBehaviour
private bool isLeaningLeft; private bool isLeaningLeft;
private bool isLeaningRight; 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;
@ -75,7 +69,6 @@ public class FirstPersonController : MonoBehaviour
private float currentCrouchHeight; private float currentCrouchHeight;
private float minCrouchHeight = 0.5f; // Minimum crouch height private float minCrouchHeight = 0.5f; // Minimum crouch height
private bool isCrouchKeyHeld; private bool isCrouchKeyHeld;
//private bool crouchHeightAdjustmentEnabled; // Flag to enable crouch height adjustment
[Header("Headbob Parameters")] [Header("Headbob Parameters")]
[SerializeField] private float walkBobSpeed = 14f; [SerializeField] private float walkBobSpeed = 14f;
@ -106,21 +99,17 @@ public class FirstPersonController : MonoBehaviour
currentCrouchHeight = crouchHeight; currentCrouchHeight = crouchHeight;
} }
// Update is called once per frame
void Update() void Update()
{ {
// Check if the inventory UI is open
bool isInventoryUIOpen = inventorySystem.IsUsingInventoryMenuUI(); bool isInventoryUIOpen = inventorySystem.IsUsingInventoryMenuUI();
if (isInventoryUIOpen) if (isInventoryUIOpen)
{ {
// The inventory UI is open, disable movement CanMove = false; // Disable movement when inventory UI is open
CanMove = false;
} }
else else
{ {
// The inventory UI is closed, enable movement CanMove = true; // Enable movement
CanMove = true;
HandleMovementInput(); HandleMovementInput();
HandleMouseLook(); HandleMouseLook();
HandleSpeedChange(); HandleSpeedChange();
@ -136,16 +125,15 @@ public class FirstPersonController : MonoBehaviour
HandleHeadbob(); HandleHeadbob();
if (canLean) if (canLean)
HandleLeaning(); // Added the HandleLeaning method call here. HandleLeaning(); // Process leaning logic
ApplyFinalMovements(); ApplyFinalMovements();
} }
} }
private void HandleMovementInput() private void HandleMovementInput()
{ {
// Determine the current speed based on the character's state (crouching, sprinting, etc.)
currentInput = new Vector2((isCrouching ? crouchSpeed : IsSprinting ? sprintSpeed : walkSpeed) * Input.GetAxis("Vertical"), (isCrouching ? crouchSpeed : IsSprinting ? sprintSpeed : walkSpeed) * Input.GetAxis("Horizontal")); currentInput = new Vector2((isCrouching ? crouchSpeed : IsSprinting ? sprintSpeed : walkSpeed) * Input.GetAxis("Vertical"), (isCrouching ? crouchSpeed : IsSprinting ? sprintSpeed : walkSpeed) * Input.GetAxis("Horizontal"));
float moveDirectionY = moveDirection.y; float moveDirectionY = moveDirection.y;
@ -156,7 +144,7 @@ public class FirstPersonController : MonoBehaviour
private void HandleJump() private void HandleJump()
{ {
if (ShouldJump) if (ShouldJump)
moveDirection.y = jumpForce; moveDirection.y = jumpForce; // Apply jump force
} }
private void HandleCrouch() private void HandleCrouch()
@ -165,12 +153,11 @@ public class FirstPersonController : MonoBehaviour
{ {
isCrouchKeyHeld = true; isCrouchKeyHeld = true;
tempCrouchHeight = currentCrouchHeight; tempCrouchHeight = currentCrouchHeight;
//crouchHeightAdjustmentEnabled = true; // Enable crouch height adjustment
} }
if (Input.GetKeyUp(crouchKey) && isCrouchKeyHeld) if (Input.GetKeyUp(crouchKey) && isCrouchKeyHeld)
{ {
// "C" key is released, apply the new crouch height // Apply the new crouch height when "C" key is released
targetCrouchHeight = tempCrouchHeight; targetCrouchHeight = tempCrouchHeight;
isCrouchKeyHeld = false; isCrouchKeyHeld = false;
StartCoroutine(CrouchStand()); StartCoroutine(CrouchStand());
@ -179,42 +166,35 @@ public class FirstPersonController : MonoBehaviour
private void HandleCrouchAdjustment() private void HandleCrouchAdjustment()
{ {
// Adjust the crouch height based on the scroll wheel input // Adjust crouch height based on mouse scroll input
float scroll = Input.GetAxis("Mouse ScrollWheel"); float scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll > 0) if (scroll > 0 && tempCrouchHeight < standingHeight)
{ {
// Increase temporary crouch height if within the maximum limit tempCrouchHeight += crouchHeightInterval;
if (tempCrouchHeight < standingHeight)
{
tempCrouchHeight += crouchHeightInterval;
}
} }
else if (scroll < 0) else if (scroll < 0 && tempCrouchHeight > minCrouchHeight)
{ {
// Decrease temporary crouch height if within the minimum limit tempCrouchHeight -= crouchHeightInterval;
if (tempCrouchHeight > minCrouchHeight)
{
tempCrouchHeight -= crouchHeightInterval;
}
} }
// Update the actual crouch height with the temporary crouch height crouchHeight = tempCrouchHeight; // Update crouch height
crouchHeight = tempCrouchHeight;
} }
private IEnumerator CrouchStand() private IEnumerator CrouchStand()
{ {
if (isCrouching && Physics.Raycast(playerCamera.transform.position, Vector3.up, 1f)) if (isCrouching && Physics.Raycast(playerCamera.transform.position, Vector3.up, 1f))
yield break; yield break;
duringCrouchAnimation = true; duringCrouchAnimation = true;
float timeElapsed = 0; float timeElapsed = 0;
float targetHeight = isCrouching ? standingHeight : currentCrouchHeight; // Use currentCrouchHeight here float targetHeight = isCrouching ? standingHeight : currentCrouchHeight;
float currentHeight = characterController.height; float currentHeight = characterController.height;
Vector3 targetCenter = isCrouching ? standingCenter : crouchingCenter; Vector3 targetCenter = isCrouching ? standingCenter : crouchingCenter;
Vector3 currentCenter = characterController.center; Vector3 currentCenter = characterController.center;
// Smooth transition between crouch and stand
while (timeElapsed < timeToCrouch) while (timeElapsed < timeToCrouch)
{ {
characterController.height = Mathf.Lerp(currentHeight, targetHeight, timeElapsed / timeToCrouch); characterController.height = Mathf.Lerp(currentHeight, targetHeight, timeElapsed / timeToCrouch);
@ -229,14 +209,14 @@ public class FirstPersonController : MonoBehaviour
isCrouching = !isCrouching; isCrouching = !isCrouching;
duringCrouchAnimation = false; duringCrouchAnimation = false;
// Update crouchHeight with currentCrouchHeight crouchHeight = currentCrouchHeight; // Update to current crouch height
crouchHeight = currentCrouchHeight;
} }
private void HandleHeadbob() private void HandleHeadbob()
{ {
if (!characterController.isGrounded) return; if (!characterController.isGrounded) return;
// Apply head bobbing effect based on movement speed and type
if (Mathf.Abs(moveDirection.x) > 0.1f || Mathf.Abs(moveDirection.z) > 0.1f) if (Mathf.Abs(moveDirection.x) > 0.1f || Mathf.Abs(moveDirection.z) > 0.1f)
{ {
timer += Time.deltaTime * (isCrouching ? crouchBobSpeed : IsSprinting ? sprintBobSpeed : walkBobSpeed); timer += Time.deltaTime * (isCrouching ? crouchBobSpeed : IsSprinting ? sprintBobSpeed : walkBobSpeed);
@ -245,7 +225,6 @@ public class FirstPersonController : MonoBehaviour
defaultYPos + Mathf.Sin(timer) * (isCrouching ? crouchBobAmount : IsSprinting ? sprintBobAmount : walkBobAmount), defaultYPos + Mathf.Sin(timer) * (isCrouching ? crouchBobAmount : IsSprinting ? sprintBobAmount : walkBobAmount),
playerCamera.transform.localPosition.z); playerCamera.transform.localPosition.z);
} }
} }
private void HandleMouseLook() private void HandleMouseLook()
@ -253,7 +232,7 @@ 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);
// Apply the current leaning angle to the camera's rotation // Apply current leaning angle to camera's rotation
playerCamera.transform.localRotation = Quaternion.Euler(rotationX + currentLeaningAngle, 0, 0); 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);
} }
@ -289,7 +268,7 @@ public class FirstPersonController : MonoBehaviour
} }
} }
// Calculate and apply the current leaning angle using the maximum leaning angle // Calculate and apply the current leaning angle
currentLeaningAngle = isLeaningLeft ? -maxLeaningAngle : isLeaningRight ? maxLeaningAngle : 0; currentLeaningAngle = isLeaningLeft ? -maxLeaningAngle : isLeaningRight ? maxLeaningAngle : 0;
} }
@ -305,25 +284,16 @@ public class FirstPersonController : MonoBehaviour
{ {
float scroll = Input.GetAxis("Mouse ScrollWheel"); float scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll > 0) // Adjust walking speed based on mouse scroll input
if (scroll > 0 && currentWalkSpeed < maxWalkSpeed)
{ {
// Increase walk speed if within the maximum limit currentWalkSpeed += walkSpeedInterval;
if (currentWalkSpeed < maxWalkSpeed)
{
currentWalkSpeed += walkSpeedInterval;
}
} }
else if (scroll < 0) else if (scroll < 0 && currentWalkSpeed > minWalkSpeed)
{ {
// Decrease walk speed if within the minimum limit currentWalkSpeed -= walkSpeedInterval;
if (currentWalkSpeed > minWalkSpeed)
{
currentWalkSpeed -= walkSpeedInterval;
}
} }
// Update the walk speed walkSpeed = currentWalkSpeed; // Update walk speed
walkSpeed = currentWalkSpeed;
} }
} }