Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Brackeys 2D Movement: Issue with crouching

Discussion in 'Getting Started' started by Adrith_Rai, Feb 24, 2024.

  1. Adrith_Rai

    Adrith_Rai

    Joined:
    Feb 23, 2024
    Posts:
    1
    I am using Unity for the game and visual studio for the code. I have a player sprite and two scripts attached for player movement. Under the player there are two more Game Objects, a ceiling check to check for ground above me and a ground check. Also, the variable m_whatIsGround has been set to a label I made: Ground.

    This is the CharacterController Script from Brackeys:

    Code (CSharp):
    1. public void Move(float move, bool crouch, bool jump)
    2. {
    3.  
    4.     // If crouching, check to see if the character can stand up
    5.     if (crouch)
    6.     {
    7.         // If the character has a ceiling preventing them from standing up, keep them crouching
    8.         if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
    9.             crouch = true;
    10.     }
    11.  
    12.     //only control the player if grounded or airControl is turned on
    13.     if (m_Grounded || m_AirControl)
    14.     {
    15.  
    16.         // If crouching
    17.         if (crouch)
    18.         {
    19.             if (!m_wasCrouching)
    20.             {
    21.                 m_wasCrouching = true;
    22.                 OnCrouchEvent.Invoke(true);
    23.             }
    24.  
    25.             // Reduce the speed by the crouchSpeed multiplier
    26.             move *= m_CrouchSpeed;
    27.  
    28.             // Disable one of the colliders when crouching
    29.             if (m_CrouchDisableCollider != null)
    30.                 m_CrouchDisableCollider.enabled = false;
    31.         } else
    32.         {
    33.             // Enable the collider when not crouching
    34.             if (m_CrouchDisableCollider != null)
    35.                 m_CrouchDisableCollider.enabled = true;
    36.  
    37.             if (m_wasCrouching)
    38.             {
    39.                 m_wasCrouching = false;
    40.                 OnCrouchEvent.Invoke(false);
    41.             }
    42.         }
    This is the PlayerMovement Script that I wrote:

    Code (CSharp):
    1. void Update()
    2. {
    3.     horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    4.  
    5.     animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
    6.  
    7.     if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
    8.     {
    9.         jump = true;
    10.         animator.SetBool("isJumping", true);
    11.     }
    12.  
    13.     if (Input.GetKeyDown(KeyCode.S))
    14.         crouch = true;
    15.     else if (Input.GetKeyUp(KeyCode.S) && !Physics2D.OverlapCircle(controller.m_CeilingCheck.position, controller.k_CeilingRadius, controller.m_WhatIsGround))
    16.         crouch = false;
    17.  
    18. }
    19.  
    20. void FixedUpdate()
    21. {
    22.     controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
    23.     jump = false;
    24. }
    25.  
    26. private Collider2D CeilingCheck()
    27. {
    28.     return Physics2D.OverlapCircle(controller.m_CeilingCheck.position, controller.k_CeilingRadius, controller.m_WhatIsGround);
    29. }
    Now, want the player to uncrouch every time I lift my ‘S’ button. The problem is, in the character controller script, the code says that every time the ceiling check overlaps the ground layer, the player should stay crouched but what I have done on my Player Controller script is, I have put a code so the player uncrouches every time I leave the ‘S’ button. I’ve been experimenting around with the code and I figured I could change the code so that the player uncrouches only if there is no ceiling on top of him and the ‘S’ button is left so it somewhat works, but now once the character walks out from under the platform, he stays crouched. I don’t know how t fix this. Could someone help?

    please let me know if any other resources are needed.