c# - 我在按住鼠标左键时无法正确播放正确的粒子效果
问题描述
我正在开发第三人称射击游戏,正如标题所述,当我按住鼠标左键时,我遇到了枪口闪光播放的粒子效果问题。即使我正在使用 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);
}
}
}
解决方案
不确定,但 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;
}
推荐阅读
- java - 在没有静态变量的情况下共享 GUI 变量
- python - 使用字符串 replace() 从数据库查询结果中获取值会导致问题
- android - 如何授予 android 应用程序用户对 AWS S3 对象的访问权限?
- android - 如何在 getView 方法中的 TextWatcher 中设置增量计数器?
- php - PHP - 检查 2 个逗号分隔的整数中的前导 0
- javascript - 为什么 [...].reduce(Math.min,0) 返回 NaN?
- java - 如何在 javers 中注册提供全局 id 字符串函数的身份类?
- javascript - 使用 Webpack 时 Socket.io 不全局可用
- javascript - Javascript 窗口未定义
- php - Symfony 4 表关系不符合命名策略?