首页 > 解决方案 > 我在按住鼠标左键时无法正确播放正确的粒子效果

问题描述

我正在开发第三人称射击游戏,正如标题所述,当我按住鼠标左键时,我遇到了枪口闪光播放的粒子效果问题。即使我正在使用 Input.GetButton 粒子效果似乎只有在我放开鼠标时才会播放。基本拍摄工作正常,我尝试使用另一种粒子效果,但只会导致同样的问题,所以我相信问题出在代码中。这段代码主要来自 Brackeys 的关于使用 Raycast 进行射击的教程,如果这样可以缩小问题的范围。我也很欣赏使用粒子系统实现枪口闪光效果的替代方案。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class FullAuto : MonoBehaviour
{
public int damage = 10;
public float range = 100f;
public float impactForce = 30f;
public float fireRate = 15f;

public int maxAmmo = 10;
public int currentAmmo;
public float reloadTime = 1f;
private bool isReloading = false;

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

private float nextTimeToFire = 0f;

public Text ammoCount;

public Animator animator;

void Start()
{
    currentAmmo = maxAmmo;
}

void OnEnable()
{
    isReloading = false;
    animator.SetBool("Reloading", false);
}

void Update()
{
    if (isReloading)
        return;

    if (currentAmmo <= 0 || Input.GetKeyDown(KeyCode.R))
    {
        StartCoroutine(Reload());
        return;
    }

    if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
    {
        muzzleFlash.Play();
        nextTimeToFire = Time.time + 1f / fireRate;
        Shoot();
    }
    ammoCount.text = "Ammo:  " + currentAmmo.ToString();
}

IEnumerator Reload()
{
    isReloading = true;
    Debug.Log("Reloading...");

    animator.SetBool("Reloading", true);

    yield return new WaitForSeconds(reloadTime - .25f);
    animator.SetBool("Reloading", false);
    yield return new WaitForSeconds(.25f);

    currentAmmo = maxAmmo;
    isReloading = false;
}

void Shoot()
{

    currentAmmo--;

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

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

}

标签: c#unity3d

解决方案


不确定,但 afaikPlay只有在粒子系统之前停止/暂停时才有效果 → 它实际上并没有重新启动粒子效果。

如果粒子系统已暂停,则从上次开始继续播放。如果粒子系统已停止,则系统从时间 0 开始,如果相关,则应用 startDelay。

我认为您宁愿使用Emit并传入每次产生多少粒子

立即发射计数粒子

public float particleAmount = 20;

void Update()
{
    if (isReloading)
        return;

    if (currentAmmo <= 0 || Input.GetKeyDown(KeyCode.R))
    {
        StartCoroutine(Reload());
        return;
    }

    if (Input.GetButton("Fire1"))
    {
        nextTimeToFire -= Time.deltaTime;

        if(nextTimeToFire <= 0)
        {
            muzzleFlash.Emit(particleAmount);
            nextTimeToFire = 1f / fireRate;
            Shoot();
        }
    }
    ammoCount.text = "Ammo:  " + currentAmmo.ToString();
}

IEnumerator Reload()
{
    isReloading = true;
    Debug.Log("Reloading...");

    animator.SetBool("Reloading", true);

    yield return new WaitForSeconds(reloadTime - .25f);
    animator.SetBool("Reloading", false);
    yield return new WaitForSeconds(.25f);

    currentAmmo = maxAmmo;
    isReloading = false;
    nextTimeToFire = 0;
}

推荐阅读