C# 粒子系统仅在GetButton()的开始和结束时播放

C# 粒子系统仅在GetButton()的开始和结束时播放,c#,unity3d,visual-studio-code,particle-system,C#,Unity3d,Visual Studio Code,Particle System,我正在制作一个FPS,下面是我的枪脚本: { public float damage = 10f; public float range = 100f; public float fireRate = 5f; public float impactForce = 30f; public Camera playerCam; public ParticleSystem muzzleFlash; public GameObject impact

我正在制作一个FPS,下面是我的枪脚本:

{
    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));
        }
    }
}
{
公众浮子损坏=10f;
公共浮动范围=100f;
公共浮式消防车的火灾率=5f;
公共浮子冲击力=30f;
公共摄像机;
公众参与制度;
公共游戏对象冲击效应;
私家花车火灾=0f;
//在第一帧更新之前调用Start
void Start()
{
}
//每帧调用一次更新
无效更新()
{
if(Input.GetButton(“Fire1”)和&Time.Time>=nextTimeToFire)
{
下一次火灾=时间。时间+1f/火灾率;
射击();
}
}
空射()
{
口吻闪光。播放();
雷卡斯特击中;
if(物理光线投射(playerCam.transform.position,playerCam.transform.forward,outhit,range))
{
Log(hit.transform.name);
Target=hit.transform.GetComponent();
如果(目标!=null)
{
目标。承受伤害(伤害);
}
if(hit.rigidbody!=null)
{
hit.刚体.AddForce(-hit.normal*冲击力);
}
实例化(impactEffect、hit.point、四元数、LookRotation(hit.normal));
}
}
}

但是,粒子系统仅在我按下鼠标或抬起鼠标时播放。当我的鼠标被按下时,它应该持续播放。请帮助我并提前感谢。

我认为这里的问题是,您只能在
Shoot()
方法中播放它,该方法仅在每次启动时调用,但我不知道您的粒子系统是什么样子。如果您想让它连续播放,应该在
Update()
中的单独If语句中将其置于
Shoot()
方法之外,或者像这样修改它:

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

我认为这里的问题是,您只在
Shoot()
方法中播放它,该方法仅在每次点火时调用,但我不知道您的粒子系统是什么样子。如果您想让它连续播放,应该在
Update()
中的单独If语句中将其置于
Shoot()
方法之外,或者像这样修改它:

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

请为您的粒子系统提供设置,我看这里没有任何问题。也许你的枪口闪光的可见效果比另一次子弹射击要晚,在这种情况下,你的粒子系统会在你看到任何东西之前通过
Play()
重置。请提供你的粒子系统的设置,我在这里看不到任何问题。也许你的枪口闪光的可见效果比另一次子弹射击要晚,在这种情况下,你的粒子系统会在你看到任何东西之前通过
Play()
重置。