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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController2D controller;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
bool crouch = false;
// Update is called once per frame
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
if (Input.GetButtonDown("Jump"))
{
jump = true;
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
}
else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
}
void FixedUpdate()
{
// Move our character
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
}
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
}
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?
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:
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
}
else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
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.
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.
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: