粒子系统仅在GetButton的开始和结束处播放

问题描述

我正在制作FPS,下面是我的GunScript:

{
    public float damage = 10f;
    public float range = 100f;
    public float fireRate = 5f;
    public float impactForce = 30f;

    public Camera playerCam;
    public ParticleSystem muzzleFlash;
    public GameObject impactEffect;

    private float nextTimetoFire = 0f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButton("Fire1") && Time.time >= nextTimetoFire)
        {
            nextTimetoFire = Time.time + 1f / fireRate;
            Shoot();
        }
    }

    void Shoot()
    {
        muzzleFlash.Play();

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

            Target target = hit.transform.GetComponent<Target>();
            if (target != null)
            {
                target.Takedamage(damage);
            }

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

            Instantiate(impactEffect,hit.point,Quaternion.LookRotation(hit.normal));
        }
    }
}

但是,只有当我按下鼠标或抬起鼠标时,粒子系统才会播放。当我按下鼠标时,它应该可以连续播放。请帮助我,谢谢。

解决方法

我认为这里的问题是,您只能在Shoot()方法中播放该方法,该方法仅在每次触发时才会调用,但我不知道您的粒子系统是什么样。如果您希望它连续播放,则应将其放在Shoot()中单独的if语句中的Update()方法之外,或像这样修改它:

if (Input.GetButton("Fire1"))
{
    muzzleFlash.Play();
    if (Time.time >= nextTimeToFire) 
    {
        nextTimeToFire = Time.time + 1f / fireRate;
        Shoot();
    }
}