I’m trying to make a first person controller and I’m not quite sure how to go about making the crouching, all I need is for the camera to be lowered and raised smoothly. I put the whole script for reference if needed.
{
public CharacterController charCon;
public float speed = 10f;
public Camera cam;
private bool isCrouching;
[Header("Running")]
public float stamina = 100f;
public float potentialConsumption;
private bool isRunning;
[Header("Jump Related")]
public float gravity = -5f;
public float groundDistance = 0.25f;
public float jumpHeight = 5f;
Vector3 velocity;
bool isGrounded;
public Transform groundCheck;
public LayerMask groundMask;
void Update()
{
StanceCheck();
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
charCon.Move(move.normalized * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump"))
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
velocity.y += gravity * Time.deltaTime;
charCon.Move(velocity * Time.deltaTime);
if(stamina > 100f)
{
stamina = 100f;
}
if(stamina < 0f)
{
stamina = 0f;
}
if (isRunning)
{
stamina -= 0.1f;
}
else
{
stamina += 0.1f;
}
}
void StanceCheck()
{
if (Input.GetKey(KeyCode.LeftShift) && stamina > 0f)
{
speed = 15f;
isRunning = true;
if (cam.fieldOfView < 66)
{
cam.fieldOfView += (5 * Time.deltaTime * 5);
}
}
else if (!isCrouching)
{
speed = 10f;
isRunning = false;
if (cam.fieldOfView > 59)
{
cam.fieldOfView += (-5 * Time.deltaTime * 5);
}
}
else if (isCrouching && !isRunning)
{
// do crouch things
speed = 5f;
}
if (Input.GetKey(KeyCode.LeftControl))
{
isCrouching = true;
}
else
{
isCrouching = false;
}
}
}