- Home /
My character won't stop crouching
Hi, i am new in unity and i am currently following this tutorial by brackeys : https://www.youtube.com/watch?v=dwcT-Dch0bA i can succesfully move, Jump, But there is a problem where when my character tries to crouch, it doesn't stop crouching, i have set my "crouch" input in unity to "w" and "down" and i also have checked the "GroundCheck" and the "CeilingCheck". So i dont know what is the problem. Here is the code
public CharacterController2D controller;
public float runSpeed = 40f;
bool jump = false;
bool crouch = false;
public float horizontalMove = 0f;
// 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.GetButtonDown("Crouch"))
{
crouch = false;
}
}
void FixedUpdate ()
{
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
}
Answer by mamuelSraz · Jul 29, 2020 at 03:40 PM
use else if (Input.GetButtonUp("Crouch")) {crouch = false;} instead of (Input.GetButtonDown("Crouch")) {crouch = false;}
Hey, I try this, but it don´t work in my code. Thats my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player$$anonymous$$ovment : $$anonymous$$onoBehaviour {
public CharacterController2D controller;
public float runSpeed = 40f;
float horizontal$$anonymous$$ove = 0f;
bool jump = false;
bool crouch = false;
// Update is called once per frame
void Update()
{
horizontal$$anonymous$$ove = 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 ()
{
// $$anonymous$$ove Character
controller.$$anonymous$$ove(horizontal$$anonymous$$ove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Making a cube crouch 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
Auto Generated pathing on a grid 0 Answers