Every time that I crouch and stand up under an object I phase through it, does anyone have a solution? I have posted about this and haven’t been able to find a solution yet.
I don’t use rigid body so here’s 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.
(I want to point out that I’m new to coding and suck at it, so I used a tutorial)
