Question by
dylanisaiahp · Apr 12 at 09:26 AM ·
c#jitteringcrouching
When I crouch and move at the same time the player jitters?
Hello, I'm relatively new to Unity and programming. Apologies if my code isn't clean or not structured correctly. Everything works as intended with my player controller (well, accept keeping momentum when you jump, but I have no idea how to do that), if I crouch and use my movement keys at the same time (WASD) I get a jitter, and I honestly can't figure out what's causing it. I've been researching all day and have come up empty handed. Any help is appreciated. Note: I have not tested this in a build just the Unity editor.
Here is my script:
using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections;
public class Player : MonoBehaviour
{
[SerializeField] CharacterController controller;
[SerializeField] Transform playerCamera;
[SerializeField] PlayerControls controls;
float mouseSensitivity = 4f;
float speed = 0f;
float walkSpeed = 5f;
float sprintSpeed = 8f;
float crouchSpeed = 3f;
float jumpHeight = 1.5f;
float crouchHeight = 1f;
float standingHeight = 2f;
float moveSmoothTime = 0.3f;
float turnSmoothTime = 0.03f;
float crouchTime = 8f;
float gravity = -20f;
float camRot = 0f;
Vector3 velocity;
Vector2 currentDirection = Vector2.zero;
Vector2 currentDirectionVelocity = Vector2.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
bool isGrounded;
bool isCrouching;
bool isCursorLocked = true;
bool isPaused = false;
private void Awake()
{
controls = new PlayerControls();
}
private void Update()
{
CheckGrouned();
if (!isPaused)
{
HandleMovement();
HandleTurning();
}
HandlePause();
CheckCursorLock();
}
private void OnEnable()
{
controls.Enable();
}
private void CheckGrouned()
{
isGrounded = Physics.Raycast(transform.position, Vector3.down, 0.08f);
Vector3 end = transform.position + (0.08f * Vector3.down);
Debug.DrawLine(transform.position, end, Color.blue);
}
private void HandleMovement()
{
float horizontal = controls.Player.Movement.ReadValue<Vector2>().x;
float vertical = controls.Player.Movement.ReadValue<Vector2>().y;
Vector2 targetDirection = new Vector2(horizontal, vertical);
currentDirection = Vector2.SmoothDamp(currentDirection, targetDirection, ref currentDirectionVelocity, moveSmoothTime);
Vector3 moveDirection = transform.right * currentDirection.x + transform.forward * currentDirection.y;
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
HandleJumping();
HandleCrouching();
UpdateSpeed();
velocity.y += gravity * Time.deltaTime;
controller.Move(Time.deltaTime * speed * moveDirection);
controller.Move(velocity * Time.deltaTime);
}
private void HandleTurning()
{
float mouseX = controls.Player.Turning.ReadValue<Vector2>().x * mouseSensitivity * Time.deltaTime;
float mouseY = controls.Player.Turning.ReadValue<Vector2>().y * mouseSensitivity * Time.deltaTime;
Vector2 targetMouseDelta = new Vector2(mouseX, mouseY);
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, turnSmoothTime);
camRot -= currentMouseDelta.y;
camRot = Mathf.Clamp(camRot, -90f, 90f);
playerCamera.transform.localRotation = Quaternion.Euler(camRot, 0, 0);
transform.Rotate(Vector3.up * currentMouseDelta.x);
}
private void HandleJumping()
{
if (controls.Player.Jump.triggered && isGrounded && !isCrouching)
{
velocity.y = Mathf.Sqrt(jumpHeight * -1.5f * gravity);
}
}
private void HandleCrouching()
{
if (controls.Player.Crouch.inProgress && isGrounded)
isCrouching = true;
else
isCrouching = false;
float desiredHeight = isCrouching ? crouchHeight : standingHeight;
if (controller.height != desiredHeight)
{
AdjustCrouch(desiredHeight);
Vector3 camPosition = playerCamera.transform.localPosition;
camPosition.y = controller.height;
playerCamera.transform.localPosition = camPosition;
}
}
private void AdjustCrouch(float height)
{
float center = height / 2;
controller.height = Mathf.Lerp(controller.height, height, crouchTime * Time.deltaTime);
controller.center = Vector3.Lerp(controller.center, new Vector3(0, center, 0), crouchTime * Time.deltaTime);
}
private void UpdateSpeed()
{
if (controls.Player.Crouch.inProgress)
{
speed = crouchSpeed;
}
else if (controls.Player.Sprint.inProgress)
{
speed = sprintSpeed;
}
else
{
speed = walkSpeed;
}
}
private void HandlePause()
{
if (controls.Player.Pause.triggered)
{
isPaused = !isPaused;
if (isPaused == false)
{
isCursorLocked = true;
}
else
{
isCursorLocked = false;
}
}
}
private void CheckCursorLock()
{
if (isCursorLocked)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
private void OnDisable()
{
controls.Disable();
}
}
Comment