I have been working on a game recently and when I crouch to go under an object I can stand up then that sends me half through the floor. I made a script that was ‘supposed’ to fix that but it didnt… Please help!
Code-
void Update() {
//Crouch
if (Input.GetKey(KeyCode.S) || (Input.GetKey(KeyCode.DownArrow))) {
speed = 5f;
gameObject.GetComponent().SetBool(“IsCrouching”, true);
GetComponent().radius = 0.3f;
GetComponent().enabled = false;
}
if (Input.GetKeyUp(KeyCode.S) || (Input.GetKeyUp(KeyCode.DownArrow))) {
if (crouch == false)
{
speed = 20f;
gameObject.GetComponent<Animator>().SetBool("IsCrouching", false);
GetComponent<CircleCollider2D>().radius = 0.6f;
GetComponent<BoxCollider2D>().enabled = true;
}
}
}
void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == ("crouch"))
{
crouch = false;
}
}
void OnCollisionStay2D(Collision2D collision)
{
if (collision.gameObject.tag == ("crouch"))
{
print("Detected");
crouch = true;
}
}
}
I have this set up to detect the collision-
I am not sure what the issue is!