Issues with crouching

Hello. I’m making a crouching in my third person 3d game. Everything is working fine until my character is passing under the object. I think the problem is in the Physics.OverlapCapsule function. Once I pressing “crouch button” under the object, no matter on which height is it, the player isn’t returning to its default state. How can I fix this? Here’s my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{

    public float moveSpeed;
    public float jumpForce;
    public CharacterController controller;
    public GameObject myCamera;

    private Vector3 moveDirection = Vector3.zero;
    private Vector3 originalCenter = Vector3.zero;
    private float originalHeight = 2f;
    private float originalMoveSpeed = 7f;
    public float gravityScale;

    public Animator anim;

    // Use this for initialization
    void Start()
    {
        transform.tag = "Player";
        controller = GetComponent<CharacterController>();
        originalCenter = Vector3.zero;
        originalHeight = 2f;
        originalMoveSpeed = 7f;
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 forwardDirection = new Vector3(myCamera.transform.forward.x, 0, myCamera.transform.forward.z);
        Vector3 sideDirection = new Vector3(myCamera.transform.right.x, 0, myCamera.transform.right.z);
        forwardDirection.Normalize();
        sideDirection.Normalize();
        forwardDirection = forwardDirection * Input.GetAxis("Vertical");
        sideDirection = sideDirection * Input.GetAxis("Horizontal");
        Vector3 directFinal = forwardDirection + sideDirection;

        if (directFinal.sqrMagnitude > 1)
        {
            directFinal.Normalize();
        }

        if (controller.isGrounded)
        {
            moveDirection = new Vector3(directFinal.x, 0, directFinal.z);
            moveDirection *= moveSpeed;
            if (Input.GetButtonDown("Jump"))
            {
                moveDirection.y = jumpForce;
            }

            if (Input.GetButtonDown("Crouch"))
            {
                controller.height = 1f;
                controller.center = new Vector3(0f, -0.5f, 0f);
                moveSpeed = 3f;
            }

            if (Input.GetButtonUp("Crouch"))
            {
                Vector3 point0 = transform.position + originalCenter - new Vector3(0f, originalHeight, 0f);
                Vector3 point1 = transform.position + originalCenter + new Vector3(0f, originalHeight, 0f);
                if (Physics.OverlapCapsule(point0, point1, controller.radius).Length == 0f)
                {
                    controller.height = originalHeight;
                    controller.center = originalCenter;
                    moveSpeed = originalMoveSpeed;
                }
            }
        }
        moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
        controller.Move(moveDirection * Time.deltaTime);

        anim.SetBool("isGrounded", controller.isGrounded);
        anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal"))));
    }
}

Thanks for any help!

Two possible things I notice:

  1. You’re only checking the height when the key is released, not every frame that it’s up. So if they release the key once and they’re still under it, they have to press and release the key again before you try to un-crouch them again.

  2. It’s possible you have point0 and point1 reversed, the docs aren’t explicitly clear for how the axes work during the cast, so I would try top to bottom instead of bottom to top like you have now.