tried to add crouch adjustments
This commit is contained in:
parent
5a841566da
commit
fb6157d752
@ -31,6 +31,7 @@ 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;
|
||||||
@ -47,6 +48,7 @@ public class FirstPersonController : MonoBehaviour
|
|||||||
[SerializeField] private float gravity = 30.0f;
|
[SerializeField] private float gravity = 30.0f;
|
||||||
|
|
||||||
[Header("Crouch Parameters")]
|
[Header("Crouch Parameters")]
|
||||||
|
[SerializeField] private float crouchHeightInterval = 0.1f;
|
||||||
[SerializeField] private float crouchHeight = 0.5f;
|
[SerializeField] private float crouchHeight = 0.5f;
|
||||||
[SerializeField] private float standingHeight = 2.0f;
|
[SerializeField] private float standingHeight = 2.0f;
|
||||||
[SerializeField] private float timeToCrouch = 0.25f;
|
[SerializeField] private float timeToCrouch = 0.25f;
|
||||||
@ -55,6 +57,13 @@ public class FirstPersonController : MonoBehaviour
|
|||||||
private bool isCrouching;
|
private bool isCrouching;
|
||||||
private bool duringCrouchAnimation;
|
private bool duringCrouchAnimation;
|
||||||
|
|
||||||
|
private float targetCrouchHeight; // The target crouch height to apply when "C" key is released
|
||||||
|
private float tempCrouchHeight; // The temporary crouch height while "C" key is held
|
||||||
|
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")]
|
[Header("Headbob Parameters")]
|
||||||
[SerializeField] private float walkBobSpeed = 14f;
|
[SerializeField] private float walkBobSpeed = 14f;
|
||||||
[SerializeField] private float walkBobAmount = 0.05f;
|
[SerializeField] private float walkBobAmount = 0.05f;
|
||||||
@ -81,7 +90,7 @@ public class FirstPersonController : MonoBehaviour
|
|||||||
Cursor.lockState = CursorLockMode.Locked;
|
Cursor.lockState = CursorLockMode.Locked;
|
||||||
Cursor.visible = false;
|
Cursor.visible = false;
|
||||||
currentWalkSpeed = walkSpeed;
|
currentWalkSpeed = walkSpeed;
|
||||||
|
currentCrouchHeight = crouchHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
@ -102,6 +111,7 @@ public class FirstPersonController : MonoBehaviour
|
|||||||
HandleMovementInput();
|
HandleMovementInput();
|
||||||
HandleMouseLook();
|
HandleMouseLook();
|
||||||
HandleSpeedChange();
|
HandleSpeedChange();
|
||||||
|
HandleCrouchAdjustment();
|
||||||
|
|
||||||
if (canJump)
|
if (canJump)
|
||||||
HandleJump();
|
HandleJump();
|
||||||
@ -135,9 +145,77 @@ public class FirstPersonController : MonoBehaviour
|
|||||||
|
|
||||||
private void HandleCrouch()
|
private void HandleCrouch()
|
||||||
{
|
{
|
||||||
if (ShouldCrouch)
|
if (Input.GetKeyDown(crouchKey) && !duringCrouchAnimation && characterController.isGrounded)
|
||||||
|
{
|
||||||
|
isCrouchKeyHeld = true;
|
||||||
|
tempCrouchHeight = currentCrouchHeight;
|
||||||
|
crouchHeightAdjustmentEnabled = true; // Enable crouch height adjustment
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Input.GetKeyUp(crouchKey) && isCrouchKeyHeld)
|
||||||
|
{
|
||||||
|
// "C" key is released, apply the new crouch height
|
||||||
|
targetCrouchHeight = tempCrouchHeight;
|
||||||
|
isCrouchKeyHeld = false;
|
||||||
StartCoroutine(CrouchStand());
|
StartCoroutine(CrouchStand());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleCrouchAdjustment()
|
||||||
|
{
|
||||||
|
// Adjust the crouch height based on the scroll wheel input
|
||||||
|
float scroll = Input.GetAxis("Mouse ScrollWheel");
|
||||||
|
|
||||||
|
if (scroll > 0)
|
||||||
|
{
|
||||||
|
// Increase temporary crouch height if within the maximum limit
|
||||||
|
if (tempCrouchHeight < standingHeight)
|
||||||
|
{
|
||||||
|
tempCrouchHeight += crouchHeightInterval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (scroll < 0)
|
||||||
|
{
|
||||||
|
// Decrease temporary crouch height if within the minimum limit
|
||||||
|
if (tempCrouchHeight > minCrouchHeight)
|
||||||
|
{
|
||||||
|
tempCrouchHeight -= crouchHeightInterval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the actual crouch height with the temporary crouch height
|
||||||
|
crouchHeight = tempCrouchHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 currentHeight = characterController.height;
|
||||||
|
Vector3 targetCenter = isCrouching ? standingCenter : crouchingCenter;
|
||||||
|
Vector3 currentCenter = characterController.center;
|
||||||
|
|
||||||
|
while (timeElapsed < timeToCrouch)
|
||||||
|
{
|
||||||
|
characterController.height = Mathf.Lerp(currentHeight, targetHeight, timeElapsed / timeToCrouch);
|
||||||
|
characterController.center = Vector3.Lerp(currentCenter, targetCenter, timeElapsed / timeToCrouch);
|
||||||
|
timeElapsed += Time.deltaTime;
|
||||||
|
yield return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
characterController.height = targetHeight;
|
||||||
|
characterController.center = targetCenter;
|
||||||
|
|
||||||
|
isCrouching = !isCrouching;
|
||||||
|
duringCrouchAnimation = false;
|
||||||
|
|
||||||
|
// Update crouchHeight with currentCrouchHeight
|
||||||
|
crouchHeight = currentCrouchHeight;
|
||||||
|
}
|
||||||
|
|
||||||
private void HandleHeadbob()
|
private void HandleHeadbob()
|
||||||
{
|
{
|
||||||
@ -170,33 +248,6 @@ public class FirstPersonController : MonoBehaviour
|
|||||||
characterController.Move(moveDirection * Time.deltaTime);
|
characterController.Move(moveDirection * Time.deltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerator CrouchStand()
|
|
||||||
{
|
|
||||||
if (isCrouching && Physics.Raycast(playerCamera.transform.position, Vector3.up, 1f))
|
|
||||||
yield break;
|
|
||||||
duringCrouchAnimation = true;
|
|
||||||
|
|
||||||
float timeElapsed = 0;
|
|
||||||
float targetHeight = isCrouching ? standingHeight : crouchHeight;
|
|
||||||
float currentHeight = characterController.height;
|
|
||||||
Vector3 targetCenter = isCrouching ? standingCenter : crouchingCenter;
|
|
||||||
Vector3 currentCenter = characterController.center;
|
|
||||||
|
|
||||||
while (timeElapsed < timeToCrouch)
|
|
||||||
{
|
|
||||||
characterController.height = Mathf.Lerp(currentHeight, targetHeight, timeElapsed / timeToCrouch);
|
|
||||||
characterController.center = Vector3.Lerp(currentCenter, targetCenter, timeElapsed);
|
|
||||||
timeElapsed += Time.deltaTime;
|
|
||||||
yield return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
characterController.height = targetHeight;
|
|
||||||
characterController.center = targetCenter;
|
|
||||||
|
|
||||||
isCrouching = !isCrouching;
|
|
||||||
|
|
||||||
duringCrouchAnimation = false;
|
|
||||||
}
|
|
||||||
private void HandleSpeedChange()
|
private void HandleSpeedChange()
|
||||||
{
|
{
|
||||||
float scroll = Input.GetAxis("Mouse ScrollWheel");
|
float scroll = Input.GetAxis("Mouse ScrollWheel");
|
||||||
|
Loading…
Reference in New Issue
Block a user