Home > Back-end >  Unity Inputs Keeps Pressed
Unity Inputs Keeps Pressed

Time:12-20

This is my shooting script, I set these in fixed update method, as it should be, but my mouse inputs keeps pressed, I am trying to make an fps game but my mouse input keeps pressed, anyone can help me? This also happens in keyboard inputs.

void FixedUpdate()
    {
        if(isReloading)
        {
            return;
        }
        if (currentAmmo <= 0)
        {
            StartCoroutine(Reload());
            return;
        }
        if(Input.GetKey(KeyCode.R))
        {
            Debug.Log("R key was pressed.");
            StartCoroutine(Reload());
            return;
        }

        if(Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
        {
            nextTimeToFire = Time.time   1f / fireRate;
            Shooting();
        }

    }
    IEnumerator Reload()
    {
        isReloading = true;
        UIController.instance.reloadMSG.gameObject.SetActive(true);
        Debug.Log("reloading");
        yield return new WaitForSeconds(reloadTime);
        currentAmmo = maxAmmo;
        isReloading = false;
        UIController.instance.reloadMSG.gameObject.SetActive(false);
    }
    private void Shooting()
    {

        currentAmmo--;
        Debug.Log("Current Ammo:"   currentAmmo);
        RaycastHit hit;
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
        {
            GameObject bulletImpactObject = Instantiate(bulletImpact, hit.point   (hit.normal * 0.002f), Quaternion.LookRotation(hit.normal, Vector3.up));
            Destroy(bulletImpactObject, 10f);
        }
        UIController.instance.ammoTXT.text = (currentAmmo   " / "   maxAmmo).ToString();
        
    }

CodePudding user response:

A few years ago I did a top-down shooter, and I had a similar problem. Try changing this:

if(Input.GetButton("Fire1") && Time.time > nextTimeToFire)
        {
            nextTimeToFire = Time.time   fireRate;
            Shooting();
        }

Removing the '=' from the comparison, and just adding the fire rate instead of dividing. That's how I did back then. Also, are you planning to do something while the gun is reloading? Because if not and it's just checking if it's reloading, you can just do this:

if !(isReloading)
{     
        if (currentAmmo <= 0)
        {
            StartCoroutine(Reload());
            return;
        }
        if(Input.GetKey(KeyCode.R))
        {
            Debug.Log("R key was pressed.");
            StartCoroutine(Reload());
            return;
        }

        if(Input.GetButton("Fire1") && Time.time > nextTimeToFire)
        {
            nextTimeToFire = Time.time   fireRate;
            Shooting();
        }
}

CodePudding user response:

Here is GunController script that does what you want. Added is a weapon recoil

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunController : MonoBehaviour
{
    public GameObject bulletPrefab; // Prefab of the bullet
    public Transform bulletSpawn; // Spawn point for the bullet
    public float fireRate = 0.5f; // Rate of fire in seconds
    public float recoil = 10f; // Recoil force applied to the player
    public int magazineSize = 6; // Size of the magazine
    public float reloadTime = 1.5f; // Time it takes to reload the gun

    private int currentAmmo; // Current ammo in the magazine
    private bool isReloading; // Flag to check if the gun is being reloaded
    private float nextFire = 0.0f; // Time when the player can fire again

    void Start()
    {
        // Initialize the current ammo to the size of the magazine
        currentAmmo = magazineSize;
    }

    void Update()
    {
        // Check if the player has pressed the fire button and if it's time to fire again
        if (Input.GetButton("Fire1") && Time.time > nextFire && !isReloading && currentAmmo > 0)
        {
            // Set the time when the player can fire again
            nextFire = Time.time   fireRate;

            // Create a bullet at the spawn point
            var bullet = (GameObject)Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);

            // Add force to the bullet
            bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.forward * 500);

            // Apply recoil force to the player
            GetComponent<Rigidbody>().AddForce(-transform.forward * recoil, ForceMode.Impulse);

            // Decrement the current ammo
            currentAmmo--;
        }

        // Check if the player has pressed the reload button and if the gun is not being reloaded
        if (Input.GetKeyDown(KeyCode.R) && !isReloading)
        {
            // Set the reload flag to true
            isReloading = true;

            // Start the reload coroutine
            StartCoroutine(Reload());
        }
    }

    IEnumerator Reload()
    {
        // Wait for the reload time
        yield return new WaitForSeconds(reloadTime);

        // Set the current ammo to the size of the magazine
        currentAmmo = magazineSize;

        // Set the reload flag to false
        isReloading = false;
    }
}

This script should be attached to the game object that represents the gun. The script has several public variables that you can customize:

  • bulletPrefab is the prefab of the bullet that will be fired.
  • bulletSpawn is the transform component of the game object that represents the spawn point for the bullet.
  • fireRate is the rate of fire in seconds. This determines how often the gun can be fired.
  • recoil is the recoil force applied to the player when the gun is fired.
  • magazineSize is the size of the magazine. This determines how many bullets the gun can hold.
  • Related