Can't get lerp working with toggleable crouch
So I found a script in this forum that lets the player crouch and lerps between standing/crouching size. The problem I'm having is that I'm wanting a toggleable crouch state, but modifying the script to include that seems to break the lerp. I can still toggle between crouching/standing and I can see the states changing in the inspector, along with the speed change working fine.
Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 6f;
public float sprintSpeed = 10f;
public float crouchSpeed = 3f;
public float jumpHeight = 1.5f;
public float gravity = -20f;
public float startHeight;
public bool isCrouched;
float maxJumpsNum = 1;
float currentJumpNum = 0;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
public bool isGrounded;
void Start()
{
controller = GetComponent<CharacterController>();
startHeight = controller.height;
}
void Update()
{
Movement();
Crouch();
Jump();
WorldHandler();
}
void Movement()
{
//w/s/a/d input.
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
//normalizes the movement vector by making its magnitude, on any of its
//axis, extend to the maximum length of 1. stops faster
//diagonal movement in this case.
Vector3 forward = transform.forward;
Vector3 right = transform.right;
forward.Normalize();
right.Normalize();
//a vector3 for the movement input - normalized.
Vector3 move = (right * x + forward * z).normalized;
if (Input.GetKey(KeyCode.LeftShift))
{
//makes the player sprint.
controller.Move(move * sprintSpeed * Time.deltaTime);
}
else
{
//makes the player move.
controller.Move(move * speed * Time.deltaTime);
}
}
void Crouch()
{
float newHeight = startHeight;
if (Input.GetKeyDown(KeyCode.C))
{
if (!isCrouched)
{
newHeight = 0.5f * startHeight;
controller.height = Mathf.Lerp(controller.height, newHeight, 5.0f * Time.deltaTime);
speed = crouchSpeed;
isCrouched = true;
}
else
{
newHeight = 1f * startHeight;
speed = 7f;
isCrouched = false;
}
}
float lastHeight = controller.height;
Vector3 position = transform.position;
position.y += (controller.height - lastHeight) * 0.5f;
}
void Jump()
{
//makes the player jump (mathf square root (h * -v * g)).
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
currentJumpNum = 0;
}
//makes the player jump again if the conditions are correct.
else if (Input.GetKeyDown(KeyCode.Space) && currentJumpNum < maxJumpsNum)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
currentJumpNum = 1;
}
}
void WorldHandler()
{
//projects a sphere from pre-defined position by [x] distance, looking for a ground layer collision.
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
//applies a tiny amount of downwards velocity constantly to help with ground checking.
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
//applies gravity to the player.
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Comment
Your answer
Follow this Question
Related Questions
Tilting a camera around an object 0 Answers
Acces shader color and lerp 1 Answer
Help with recttransform lerp , lags on mobile not on pc. 1 Answer
Can you make the player do a 180° / u-turn when a button is pressed? 0 Answers
Rigid body vibrating? 0 Answers