粒子系统仅在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()方法之外的Update()中的一个单独的If语句中,或者像这样修改它:

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

最新更新