【发布时间】:2014-10-16 08:21:04
【问题描述】:
所以我将这个脚本附加到游戏中,并且我添加的 While 循环应该跟踪弹药并在每次我发射火箭时下降 1,但是当我在游戏中左键单击(射击)时,它会将我所有的弹药射向一次。我的代码:
public class CreateRocket : MonoBehaviour {
public Rigidbody rocket;
public float speed = 10f;
public int aantalRaketten;
public int Ammo = 10;
// Use this for initialization
void Start () {}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
FireRocket();
}
}
void FireRocket()
{
while (Ammo >= aantalRaketten)
{
Ammo--;
Rigidbody rocketClone = (Rigidbody)Instantiate(rocket, transform.position + transform.forward * 2, transform.rotation);
rocketClone.velocity = transform.forward * speed;
}
}
}
谢谢!
【问题讨论】:
-
您的标题无法解释您的问题。 Write a better one 代替。
-
嗯...是的! while 循环基本上立即运行完成......
-
在while循环中设置断点并开始调试。当程序在循环内停止时,条件至少符合一次。如果这不是您的问题,请说明问题。
-
如果你只想每次点击发射一枚火箭,你应该使用
if而不是while。 -
我先有一个“if”,但任务是使用 while 循环。
标签: c# loops while-loop