I’m posting about this for the third time because I haven’t been able to find a solution. Whenever I crouch, I am able to stand up under walls and clip through them. People have tried their best at helping and have gotten me to realize a bit of what is happening, but first, the code.
public class PlayerMotor : MonoBehaviour
{
private CharacterController controller;
private Vector3 playerVelocity;
public float speed = 2f;
private bool isGrounded;
public float gravity = -9.8f;
bool lerpCrouch = false;
bool crouching = false;
bool sprinting = false;
float crouchTimer;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
isGrounded = controller.isGrounded;
if (lerpCrouch)
{
crouchTimer += Time.deltaTime;
float p = crouchTimer / 1;
p *= p;
if (crouching)
controller.height = Mathf.Lerp(controller.height, 1, p);
else
controller.height = Mathf.Lerp(controller.height, 2, p);
if (p > 1)
{
lerpCrouch = false;
crouchTimer = 0f;
}
}
}
public void Crouch()
{
crouching = !crouching;
crouchTimer = 0;
lerpCrouch = true;
if (crouching && sprinting)
speed = 1;
if (crouching)
speed = 1;
else
speed = 2;
}
public void Sprint()
{
sprinting = !sprinting;
if (sprinting)
speed = 4;
else
speed = 2;
if (crouching && sprinting)
speed = 1;
}
public void ProcessMove(Vector2 input)
{
Vector3 moveDirection = Vector3.zero;
moveDirection.x = input.x;
moveDirection.z = input.y;
controller.Move(transform.TransformDirection(moveDirection) * speed * Time.deltaTime);
playerVelocity.y += gravity * Time.deltaTime;
if (isGrounded && playerVelocity.y < 0)
playerVelocity.y = -2f;
controller.Move(playerVelocity * Time.deltaTime);
Debug.Log(playerVelocity.y);
}
}
There is no standing option, only a camera position as well as a crouching action. As a previous user mentioned, using ray cast should have done the trick but it didn’t work. This has halted my progress for months and hope someone knows how to help. Thank you.