fps controller changes
This commit is contained in:
parent
1931283255
commit
644bce2a17
@ -1,14 +1,13 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
using SimpleInventorySystem;
|
||||
|
||||
public class FirstPersonController : MonoBehaviour
|
||||
{
|
||||
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;
|
||||
private bool IsSprinting => canSprint && Input.GetKey(sprintKey);
|
||||
private bool ShouldJump => Input.GetKeyDown(jumpKey) && characterController.isGrounded;
|
||||
@ -21,7 +20,6 @@ public class FirstPersonController : MonoBehaviour
|
||||
[SerializeField] private bool canUseHeadbob = true;
|
||||
[SerializeField] private bool canLean = true;
|
||||
|
||||
|
||||
[Header("Controls")]
|
||||
[SerializeField] private KeyCode sprintKey = KeyCode.LeftShift;
|
||||
[SerializeField] private KeyCode jumpKey = KeyCode.Space;
|
||||
@ -33,19 +31,16 @@ public class FirstPersonController : MonoBehaviour
|
||||
[SerializeField] private float walkSpeedInterval = 0.5f;
|
||||
private float currentWalkSpeed;
|
||||
|
||||
|
||||
[SerializeField] private float walkSpeed = 3.0f;
|
||||
[SerializeField] private float sprintSpeed = 6.0f;
|
||||
[SerializeField] private float crouchSpeed = 1.5f;
|
||||
|
||||
|
||||
[Header("Look Parameters")]
|
||||
[SerializeField, Range(1, 10)] private float lookSpeedX = 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 lowerLookLimit = 80.0f;
|
||||
|
||||
|
||||
[Header("Leaning Parameters")]
|
||||
[SerializeField] private bool toggleLeaning = false; // Toggle mode
|
||||
[SerializeField] private KeyCode leanLeftKey = KeyCode.Q;
|
||||
@ -55,7 +50,6 @@ public class FirstPersonController : MonoBehaviour
|
||||
private bool isLeaningLeft;
|
||||
private bool isLeaningRight;
|
||||
|
||||
|
||||
[Header("Jumping Parameters")]
|
||||
[SerializeField] private float jumpForce = 8.0f;
|
||||
[SerializeField] private float gravity = 30.0f;
|
||||
@ -75,7 +69,6 @@ public class FirstPersonController : MonoBehaviour
|
||||
private float currentCrouchHeight;
|
||||
private float minCrouchHeight = 0.5f; // Minimum crouch height
|
||||
private bool isCrouchKeyHeld;
|
||||
//private bool crouchHeightAdjustmentEnabled; // Flag to enable crouch height adjustment
|
||||
|
||||
[Header("Headbob Parameters")]
|
||||
[SerializeField] private float walkBobSpeed = 14f;
|
||||
@ -106,21 +99,17 @@ public class FirstPersonController : MonoBehaviour
|
||||
currentCrouchHeight = crouchHeight;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
// Check if the inventory UI is open
|
||||
bool isInventoryUIOpen = inventorySystem.IsUsingInventoryMenuUI();
|
||||
|
||||
if (isInventoryUIOpen)
|
||||
{
|
||||
// The inventory UI is open, disable movement
|
||||
CanMove = false;
|
||||
CanMove = false; // Disable movement when inventory UI is open
|
||||
}
|
||||
else
|
||||
{
|
||||
// The inventory UI is closed, enable movement
|
||||
CanMove = true;
|
||||
CanMove = true; // Enable movement
|
||||
HandleMovementInput();
|
||||
HandleMouseLook();
|
||||
HandleSpeedChange();
|
||||
@ -136,16 +125,15 @@ public class FirstPersonController : MonoBehaviour
|
||||
HandleHeadbob();
|
||||
|
||||
if (canLean)
|
||||
HandleLeaning(); // Added the HandleLeaning method call here.
|
||||
HandleLeaning(); // Process leaning logic
|
||||
|
||||
ApplyFinalMovements();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
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"));
|
||||
|
||||
float moveDirectionY = moveDirection.y;
|
||||
@ -156,7 +144,7 @@ public class FirstPersonController : MonoBehaviour
|
||||
private void HandleJump()
|
||||
{
|
||||
if (ShouldJump)
|
||||
moveDirection.y = jumpForce;
|
||||
moveDirection.y = jumpForce; // Apply jump force
|
||||
}
|
||||
|
||||
private void HandleCrouch()
|
||||
@ -165,12 +153,11 @@ public class FirstPersonController : MonoBehaviour
|
||||
{
|
||||
isCrouchKeyHeld = true;
|
||||
tempCrouchHeight = currentCrouchHeight;
|
||||
//crouchHeightAdjustmentEnabled = true; // Enable crouch height adjustment
|
||||
}
|
||||
|
||||
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;
|
||||
isCrouchKeyHeld = false;
|
||||
StartCoroutine(CrouchStand());
|
||||
@ -179,42 +166,35 @@ public class FirstPersonController : MonoBehaviour
|
||||
|
||||
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");
|
||||
|
||||
if (scroll > 0)
|
||||
{
|
||||
// Increase temporary crouch height if within the maximum limit
|
||||
if (tempCrouchHeight < standingHeight)
|
||||
if (scroll > 0 && tempCrouchHeight < standingHeight)
|
||||
{
|
||||
tempCrouchHeight += crouchHeightInterval;
|
||||
}
|
||||
}
|
||||
else if (scroll < 0)
|
||||
{
|
||||
// Decrease temporary crouch height if within the minimum limit
|
||||
if (tempCrouchHeight > minCrouchHeight)
|
||||
else if (scroll < 0 && tempCrouchHeight > minCrouchHeight)
|
||||
{
|
||||
tempCrouchHeight -= crouchHeightInterval;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the actual crouch height with the temporary crouch height
|
||||
crouchHeight = tempCrouchHeight;
|
||||
crouchHeight = tempCrouchHeight; // Update crouch height
|
||||
}
|
||||
|
||||
private IEnumerator CrouchStand()
|
||||
{
|
||||
if (isCrouching && Physics.Raycast(playerCamera.transform.position, Vector3.up, 1f))
|
||||
yield break;
|
||||
|
||||
duringCrouchAnimation = true;
|
||||
|
||||
float timeElapsed = 0;
|
||||
float targetHeight = isCrouching ? standingHeight : currentCrouchHeight; // Use currentCrouchHeight here
|
||||
float targetHeight = isCrouching ? standingHeight : currentCrouchHeight;
|
||||
float currentHeight = characterController.height;
|
||||
Vector3 targetCenter = isCrouching ? standingCenter : crouchingCenter;
|
||||
Vector3 currentCenter = characterController.center;
|
||||
|
||||
// Smooth transition between crouch and stand
|
||||
while (timeElapsed < timeToCrouch)
|
||||
{
|
||||
characterController.height = Mathf.Lerp(currentHeight, targetHeight, timeElapsed / timeToCrouch);
|
||||
@ -229,14 +209,14 @@ public class FirstPersonController : MonoBehaviour
|
||||
isCrouching = !isCrouching;
|
||||
duringCrouchAnimation = false;
|
||||
|
||||
// Update crouchHeight with currentCrouchHeight
|
||||
crouchHeight = currentCrouchHeight;
|
||||
crouchHeight = currentCrouchHeight; // Update to current crouch height
|
||||
}
|
||||
|
||||
private void HandleHeadbob()
|
||||
{
|
||||
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)
|
||||
{
|
||||
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),
|
||||
playerCamera.transform.localPosition.z);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void HandleMouseLook()
|
||||
@ -253,7 +232,7 @@ public class FirstPersonController : MonoBehaviour
|
||||
rotationX -= Input.GetAxis("Mouse Y") * lookSpeedY;
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
@ -305,25 +284,16 @@ public class FirstPersonController : MonoBehaviour
|
||||
{
|
||||
float scroll = Input.GetAxis("Mouse ScrollWheel");
|
||||
|
||||
if (scroll > 0)
|
||||
{
|
||||
// Increase walk speed if within the maximum limit
|
||||
if (currentWalkSpeed < maxWalkSpeed)
|
||||
// Adjust walking speed based on mouse scroll input
|
||||
if (scroll > 0 && currentWalkSpeed < maxWalkSpeed)
|
||||
{
|
||||
currentWalkSpeed += walkSpeedInterval;
|
||||
}
|
||||
}
|
||||
else if (scroll < 0)
|
||||
{
|
||||
// Decrease walk speed if within the minimum limit
|
||||
if (currentWalkSpeed > minWalkSpeed)
|
||||
else if (scroll < 0 && currentWalkSpeed > minWalkSpeed)
|
||||
{
|
||||
currentWalkSpeed -= walkSpeedInterval;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the walk speed
|
||||
walkSpeed = currentWalkSpeed;
|
||||
walkSpeed = currentWalkSpeed; // Update walk speed
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user