Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Crouch script issues

Discussion in 'Scripting' started by TheUltimateRealDude1, Oct 2, 2019.

  1. TheUltimateRealDude1

    TheUltimateRealDude1

    Joined:
    Sep 27, 2019
    Posts:
    10
    Hello! I have been using the 2d movement in Unity tutorial by Brackeys, and I have encountered a big problem. The crouch is always on. I can't turn it off, so i can easily move under anything. Please help me figure out what is wrong! I followed the instructions completely (to my knowledge). Here is my code:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3.  
    4. using System.Collections.Generic;
    5.  
    6. using UnityEngine;
    7.  
    8.  
    9.  
    10. public class PlayerMovement : MonoBehaviour
    11. {
    12.  
    13.  
    14.     public CharacterController2D controller;
    15.  
    16.  
    17.  
    18.     public float runSpeed = 40f;
    19.  
    20.  
    21.  
    22.     float horizontalMove = 0f;
    23.     bool jump = false;
    24.     bool crouch = false;
    25.  
    26.  
    27.  
    28.     // Update is called once per frame
    29.  
    30.     void Update()
    31.     {
    32.  
    33.  
    34.  
    35.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    36.  
    37.  
    38.  
    39.         if (Input.GetButtonDown("Jump"))
    40.  
    41.         {
    42.  
    43.             jump = true;
    44.  
    45.         }
    46.  
    47.        
    48.  
    49.         if (Input.GetButtonDown("Crouch"))
    50.  
    51.         {
    52.  
    53.             crouch = true;
    54.  
    55.            
    56.         }
    57.  
    58.         else if (Input.GetButtonUp("Crouch"))
    59.  
    60.         {
    61.  
    62.             crouch = false;
    63.  
    64.         }
    65.  
    66.  
    67.     }
    68.  
    69.  
    70.     void FixedUpdate()
    71.  
    72.     {
    73.  
    74.         // Move our character
    75.  
    76.         controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
    77.  
    78.         jump = false;
    79.  
    80.  
    81.     }
    82.  
    83. }
    84.  
    85. }
     
    Last edited: Oct 2, 2019
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Code (CSharp):
    1. if (Input.GetButtonDown("Crouch"))
    2.         {
    3.             crouch = true;
    4.             if (Input.GetButtonUp("Crouch"))
    5.             {
    6.                 crouch = false;
    7.             }
    8.         }
    What's the exact condition that causes crouch=false? I think you'll discover an impossible set of conditions if you think about it. ;) What state must the button be in in order to get inside the second scope block?
     
  3. TheUltimateRealDude1

    TheUltimateRealDude1

    Joined:
    Sep 27, 2019
    Posts:
    10
    Oops I just realized that I posted an edited version of my script that I made to try to debug my main script.
    I updated it in my main script too, but here is the edit area:
    Code (CSharp):
    1.  
    2.  if (Input.GetButtonDown("Crouch"))
    3.  
    4.         {
    5.  
    6.             crouch = true;
    7.  
    8.            
    9.         }
    10.  
    11.         else if (Input.GetButtonUp("Crouch"))
    12.  
    13.         {
    14.  
    15.             crouch = false;
    16.  
    17.         }
    18.  
     
  4. TheUltimateRealDude1

    TheUltimateRealDude1

    Joined:
    Sep 27, 2019
    Posts:
    10
    Yay! I figured it out! In the Character Controller 2D script there is a place that says !crouch. You just need to change it to crouch!
     
    gregspcht and FuriousDevil like this.
  5. FuriousDevil

    FuriousDevil

    Joined:
    May 31, 2020
    Posts:
    2
    omg, @TheUltimateRealDude1 , it worked, I have been like wtf just happened for several hours now. It was that line. Thanks!
     
  6. TheUltimateRealDude1

    TheUltimateRealDude1

    Joined:
    Sep 27, 2019
    Posts:
    10
    I'm glad that my past self could help you out! Haha
     
  7. FuriousDevil

    FuriousDevil

    Joined:
    May 31, 2020
    Posts:
    2
    Btw after some more digging it turned out this was not the correct fix, I had to remove the character itself from the things it collided with. Now it can actually crouch properly and is not glitching while walking.
     
  8. tournevis234

    tournevis234

    Joined:
    Mar 18, 2020
    Posts:
    1
    Hey FuriousDevil, could you elaborate on that? What exactly do you mean by "removing the character itself from the things it collided with"? I have the same problem where crouching is always on.
     
    Nikori likes this.
  9. Nikori

    Nikori

    Joined:
    Jan 30, 2021
    Posts:
    1
    Did you end up fixing the problem? I'm having the same issue and I'm using the same tutorial on YouTube... I found out my character is constantly crouching... D: