hey so while i was making my game i encountered a bug when i start my game i cant sprint,jump or wall run but when i press cltr for crouching after crouching every thing works completely fine
heres my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
float playerHeight = 2f;
[SerializeField] Transform orientation;
[Header("Movement")]
public float moveSpeed = 6f;
public float movementMultiplier = 10f;
[SerializeField] float airMultiplier = 0.4f;
[Header("Sprinting")]
[SerializeField] float walkSpeed = 4f;
[SerializeField] float SprintSpeed = 6f;
[SerializeField] float acceleration = 10f;
bool ISsprinting;
[Header("crouch")]
[SerializeField] private Vector3 crouchScale = new Vector3(1, 0.5f, 1);
[SerializeField] private Vector3 playerScale;
[Header("Jumping")]
public float jumpForce = 5f;
[Header("Keybinds")]
[SerializeField] KeyCode jumpKey = KeyCode.Space;
[SerializeField] KeyCode SprintKey = KeyCode.LeftShift;
[Header("Drag")]
float groundDrag = 6f;
float airDrag = 2f;
public float rbDrag = 6f;
float horizontalMovement;
float verticalMovement;
[Header("ground Detection")]
[SerializeField] Transform groundCheck;
[SerializeField] LayerMask groundMask;
bool isGrounded;
bool isjumping;
float groundDistamce = 0.4f;
Vector3 moveDirection;
Vector3 slopeDirection;
Rigidbody rb;
RaycastHit slopeHit;
private bool OnSlope()
{
if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2 + 0.5f))
{
if(slopeHit.normal != Vector3.up)
{
return true;
}
else
{
return false;
}
}
return false;
}
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
private void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistamce,groundMask );
MyInput();
ControlDrag();
ControlSpeed();
if(Input.GetKeyDown(jumpKey) && isGrounded)
{
Jump();
}
slopeDirection = Vector3.ProjectOnPlane(moveDirection, slopeHit.normal);
}
void MyInput()
{
horizontalMovement = Input.GetAxisRaw("Horizontal");
verticalMovement = Input.GetAxisRaw("Vertical");
moveDirection = orientation.forward * verticalMovement + orientation.right * horizontalMovement;
if (Input.GetKeyDown(KeyCode.LeftControl))
StartCrouch();
if (Input.GetKeyUp(KeyCode.LeftControl))
StopCrouch();
}
private void StartCrouch()
{
transform.localScale = crouchScale;
transform.position = new Vector3(transform.position.x, transform.position.y - 0.5f, transform.position.z);
isjumping = false;
ISsprinting = false;
walkSpeed = 3f;
}
private void StopCrouch()
{
transform.localScale = playerScale;
transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
isjumping = true;
ISsprinting = true;
walkSpeed = 6f;
}
void Jump()
{
if (isGrounded && isjumping)
{
rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
}
void ControlSpeed()
{
if(Input.GetKey(SprintKey) && isGrounded && ISsprinting)
{
moveSpeed = Mathf.Lerp(moveSpeed, SprintSpeed, acceleration * Time.deltaTime);
}
else
{
moveSpeed = Mathf.Lerp(moveSpeed, walkSpeed, acceleration * Time.deltaTime);
}
}
void ControlDrag()
{
if (isGrounded)
{
rb.drag = groundDrag;
}
else
{
rb.drag = airDrag;
}
}
private void FixedUpdate()
{
MovePlayer();
}
void MovePlayer()
{
if (isGrounded && !OnSlope())
{
rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
}
else if (isGrounded && OnSlope())
{
rb.AddForce(slopeDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
}
else if (!isGrounded)
{
rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
}
}
}