【发布时间】:2021-02-20 15:39:44
【问题描述】:
使用 c#,我尝试每 3 秒发射一次子弹,所以这是我的工作流程:
- 按下触发按钮
- 只有当 bool fireAgain 为真时才会触发
- 设置 bool fireAgain = false
- 启动计时器
- 计时器完成 = bool fireAgain = true
调试时似乎一切正常,但是当我测试它时,我仍然可以每秒发射 10 发子弹。所以不知何故,它只是不关心 bool FireAgain 是否为假,并且无论如何都会拍摄,即使根据调试 bool fireAgain 在那一刻是假的。
public void Update()
{
//If LEFT MouseButton is pressed, cast yer spells.
if (fireAgain == true && Input.GetMouseButtonDown(0))
{
StartCoroutine("LoopRotation");
fireAgain = false;
Debug.Log(fireAgain);
Debug.Log("1 should be FALSE");
}
while (fireAgain == false && timer < bulletTime)
{
fireAgain = false;
timer += Time.deltaTime;
Debug.Log(timer);
Debug.Log(bulletTime);
Debug.Log(fireAgain);
Debug.Log("2");
} if (timer >= bulletTime)
{
fireAgain = true;
timer = 0;
//Debug.Log("Timer is finished");
//Debug.Log(timer);
Debug.Log(fireAgain);
Debug.Log("3 should be true");
这是协程的代码:
IEnumerator LoopRotation()
{
pivot.transform.Rotate(triggerAngle,0,0);
GameObject bullet = ObjectPooler.SharedInstance.GetPooledObject();
if (bullet != null) {
bullet.transform.position = SSpawn.transform.position;
bullet.transform.rotation = SSpawn.transform.rotation;
bullet.SetActive(true);
}
yield return new WaitForSeconds(.1f);
pivot.transform.rotation = Quaternion.Slerp(transform.rotation, originalRotationValue, Time.deltaTime * rotationResetSpeed);
StopCoroutine("LoopRotation");
}
Enumerator LoopRotation 最初只是将武器向前旋转几度,然后向后旋转几度,所以当你施法时它看起来像个怪人,但现在它也是射击功能,因为它会产生子弹。
【问题讨论】:
标签: c# unity3d 3d frame-rate