crouch script problem

So I’m Trying to make my player crouch,
this is the script:

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

    public class PlayerMovement :
    MonoBehaviour {
    public ParticleSystem dust;
    private bool isCrouching;
    [SerializeField] private float speed;
    [SerializeField] private LayerMask groundLayer;
    private Rigidbody2D rb;
    private Animator anim;
    private BoxCollider2D boxCollider;
    private int horizontalInput;

    private void Awake()
    {
        //Grab references for rigidbody and animator from object
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        boxCollider = GetComponent<BoxCollider2D>();
    }
    
    private void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(horizontalInput * speed,
    

    rb.velocity.y);
    //flip player when moving left-right
    CreateDust();
    if (horizontalInput > 0.06f)
    transform.localScale = new Vector3(8, 8, 1);
    else if (horizontalInput < -0.06f)
    transform.localScale = new Vector3(-8, 8, 1);

        if (Input.GetKey(KeyCode.Space) &&
    

    isGrounded())
    Jump();

        if (Input.GetKeyDown(KeyCode.S))
        {
            boxCollider.enabled = false;
            boxCollider.size = new Vector3(2, 2, 1);
            anim.SetBool("Crouch", true);
        }
        else if (Input.GetKeyUp(KeyCode.S))
        {
            boxCollider.enabled = true;
            anim.SetBool("Crouch", false);
        }
    
    
        //Set animator parameters
        anim.SetBool("run", horizontalInput != 0);
        anim.SetBool("grounded", isGrounded());
    }
    
    private void Jump()
    {
        rb.velocity = new Vector2(rb.velocity.x, speed);
        anim.SetTrigger("jump");
    }
    
    private bool isGrounded()
    {
        RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center,
    

    boxCollider.bounds.size, 0,
    Vector2.down, 0.1f, groundLayer);
    return raycastHit.collider != null;
    }

    public bool canAttack()
    {
        return horizontalInput == 0 && isGrounded();
    }
    
    public bool canKick()
    {
        return horizontalInput == 0 && isGrounded();
    }
    
    void CreateDust()
    {
        dust.Play();
    } }
    

and the box colliders weren’t keeping my character from falling through the ground, and I don’t understand how to implement it.

if (Input.GetKeyDown(KeyCode.S))
{
boxCollider.enabled = false;
boxCollider.size = new Vector3(2, 2, 1);
anim.SetBool(“Crouch”, true);
}

you turn off the player’s box collider with boxCollider.enable = false;. that’s why it falls through the ground. for collisions to work, you need collider on the object you need collisions to work on. you have to resize the collider to crouch position when implementing crouch instead of turning box collider off, and you actually did it with boxCollider.size = new Vector3(2,2,1);

removing boxCollider.enable = false; should be enough. also, don’t forget to resize box collider when player stands up.