How to disable keyboard key with script

Hello.How I can disable a key on keyboard in a script. I want to make that when im relaoding so click “R” key than disable mouse0 key – not functional. Is this possible? :slight_smile:
Thanks in advance! :smiley:

I hope this helps you out:


Another answer related to this:

…deleted due to spam content…

Please post any code you may have already so we can see what you’ve accomplished thus far. Thanks!

you could try below steps:

  1. add a bool canFire; which by default is true.

  2. In the Input.GetMousebutton(0)/whatever is your if-check for firing weapon, add a condition requiring canFire to be true as well.

  3. Now right after/before your reload animation code, set canFire to false.

  4. Start a coroutine right after this, with yield return just until the reload animation finishes and set the bool canFIre to true.

This way, the canFire bool will be set to True only after the coroutine finishes the assigned number of seconds(which is the reload animation duration). As long as canFire is false, your fire weapon command will not be called.

Hope this helps.

This is my code:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class Weapon : MonoBehaviour
{
[Header(“Gun Settings”)]
public float fireRate = 0.1f;
public int clipSize = 30;
public int reservedAmmoCapacity = 270;

public float damage = 10f;
public float range = 100;
public float impactForce = 30f;



public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
public GameObject Crosshair;




//Variable that change throughout code
bool _canShoot;
int _currentAmmoInClip;
int _ammoInReserve;

//Aiming
public Vector3 normalLocalPosition;
public Vector3 aimingLocalPosition;

public float aimSmoothing = 10;

//Weapon Recoil

public bool randomizeRecoil;
public Vector2 randomRecoilConstraints;

public Vector2 recoilPattern;

[Header("Mouse settins")]
public float mouseSensitivity = 1;
Vector2 _currentRotation;

private void Start()
{
    _currentAmmoInClip = clipSize;
    _ammoInReserve = reservedAmmoCapacity;
    _canShoot = true;
}

private void Update()
{
    DetermineAim();
    if (Input.GetMouseButton(0) && _canShoot && _currentAmmoInClip > 0)
    {
        _canShoot = false;
        _currentAmmoInClip--;
        StartCoroutine(ShootGun());
    }

    else if (Input.GetKeyDown(KeyCode.R) && _currentAmmoInClip < clipSize && _ammoInReserve > 0)
    {
        int amountNeeded = clipSize - _currentAmmoInClip;

        if (amountNeeded >= _ammoInReserve)
        {
            _currentAmmoInClip += _ammoInReserve;
            _ammoInReserve -= amountNeeded;
        }
        
        else
        {
            _currentAmmoInClip = clipSize;
            _ammoInReserve -= amountNeeded;

        }


    }

    //When click "reload button" disable shooting and then set back to can shoot after x seconds
    bool reloading = (Input.GetKeyDown(KeyCode.R));

    if (reloading)
    {
        _canShoot = false;
        StartCoroutine(StopShooting());
    }

    IEnumerator StopShooting()
    {
        yield return new WaitForSeconds(1.4f);
        _canShoot = true;

    }

}



void DetermineRecoil()
{
    transform.localPosition -= Vector3.forward * 0.1f;

    if (randomizeRecoil)
    {
        float xRecoil = Random.Range(-randomRecoilConstraints.x, randomRecoilConstraints.x);
        float yRecoil = Random.Range(-randomRecoilConstraints.y, randomRecoilConstraints.y);

        Vector2 recoil = new Vector2(xRecoil, yRecoil);

        
    }

    else
    {
        int currentStep = clipSize + 1 - _currentAmmoInClip;
        

        
    }
}



void DetermineAim()
{
    bool Aiming = (Input.GetKey(KeyCode.Mouse1));
    Vector3 target = normalLocalPosition;
    if (Input.GetMouseButton(1)) target = aimingLocalPosition;

    Vector3 desiredPosition = Vector3.Lerp(transform.localPosition, target, Time.deltaTime * aimSmoothing);

    transform.localPosition = desiredPosition;

    if(Aiming)
    {
        Crosshair.SetActive(false);
    }
    
}



IEnumerator ShootGun()
{
    DetermineRecoil();
    yield return new WaitForSeconds(fireRate);
    _canShoot = true;

    muzzleFlash.Play();

    RaycastHit hit;
    if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    {

        Enemy enemy = hit.transform.GetComponent<Enemy>();
        if (enemy != null)
        {
            enemy.TakeDamage(damage);
        }

        if (hit.rigidbody != null)
        {
            hit.rigidbody.AddForce(-hit.normal * impactForce);
        }

        GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
        Destroy(impactGO, 2f);

    }

}

}