- Home /
How to stop crouching automaticaly after getting out of a small area
So im making a 2d game and i can crouch, i can stop crouching, and if im under a sprite with the tag ground im forced to continue crouching.
I want to make that if u get out under the sprite tagged ground automaticaly set it back to not be crouching anymore
Sorry i cannot explain it better im useless with english XD if u have any questions or u do not understand smth tell me
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Crouch : MonoBehaviour
{
public Transform celCheck;
public float checkRadius;
public LayerMask whatIsGround;
public bool isCel;
public CapsuleCollider2D col;
public bool crouching;
public PlayerController player;
// Start is called before the first frame update
void Start()
{
crouching = false;
isCel = false;
col = GetComponent<CapsuleCollider2D>();
player = GetComponent<PlayerController>();
}
// Update is called once per frame
void Update(){
isCel = Physics2D.OverlapCircle(celCheck.position, checkRadius, whatIsGround);
if (!isCel) {
if (Input.GetButtonDown("Crouch")) {
col.enabled = false;
player.speed /= 2;
crouching = true;
} else if (Input.GetButtonUp("Crouch")) {
col.enabled = true;
player.speed = player.speedValue;
crouching = false;
}
} else if (!isCel && crouching) {
crouching = false;
col.enabled = true;
}
}
}
Comment