unity3d Unity输入保持按下

vybvopom  于 2022-12-23  发布在  其他
关注(0)|答案(2)|浏览(273)

这是我的拍摄脚本,我设置这些在固定的更新方法,因为它应该是,但我的鼠标输入一直按下,我试图使一个fps游戏,但我的鼠标输入一直按下,有人能帮助我吗?这也发生在键盘输入。

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();
        
    }
rjee0c15

rjee0c151#

几年前我做了一个自顶向下的射手,我有一个类似的问题。尝试改变这个:

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

从比较中去掉“=”,只把射速相加而不是相除。我当时就是这么做的。还有,你打算在枪重新装弹的时候做些什么吗?因为如果没有,它只是检查它是否在重新装弹,你可以这样做:

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();
        }
}
niknxzdl

niknxzdl2#

这是一个GunController脚本,可以做你想做的事情。增加了武器后坐力

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;
    }
}

这个脚本应该附加到代表枪的游戏对象上。这个脚本有几个公共变量可以自定义:

  • bulletPrefab是将要发射的子弹的预制件。
  • bulletSpawn是表示子弹的产生点的游戏对象的变换组件。
  • fireRate是以秒为单位的射速。这决定了火炮可以发射的频率。
  • recoil是当枪发射时施加到玩家的后坐力。
  • magazineSize是杂志的大小。这决定了枪能装多少子弹。

相关问题