当我制造炸弹的时候它会产生更多,有时甚至达到3bombs,我希望它制造1枚炸弹而不是2或3枚炸弹。 我需要一种方法来确保它只使用一次虚空掉落炸弹。 我期待着你的帮助,我想为那些糟糕的英语和maby说我可能错过了我的代码中的重要内容
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaneBombDroper : MonoBehaviour
{
public GameObject bomb;
public GameObject bombDropPostion;
public GameObject planePostion;
int bombDropRandomNum;
public float[] dropPostionsX;
bool bombIsDroped;
// Use this for initialization
void Start()
{
bombDropRandomNum = Random.Range(1, 3);
}
void Update()
{
if (bombDropRandomNum == 1 && bombIsDroped != true)
{
if (planePostion.transform.position.x < -2.75f && planePostion.transform.position.x > -3)
{
dropBomb();
}
}
if (bombDropRandomNum == 2&& bombIsDroped != true)
{
if (planePostion.transform.position.x < -9.5 && planePostion.transform.position.x > -10)
{
StartCoroutine("WaitForSeconds");
StopCoroutine("WaitForSeconds");
}
}
}
void dropBomb()
{
Instantiate(bomb, gameObject.transform.position, gameObject.transform.rotation);
}
IEnumerator WaitForSeconds()
{
dropBomb();
yield return new WaitForSeconds(1);
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
只需在更新中添加标记
即可using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaneBombDroper : MonoBehaviour
{
public GameObject bomb;
public GameObject bombDropPostion;
public GameObject planePostion;
int bombDropRandomNum;
public float[] dropPostionsX;
bool bombIsDroped;
private bool launchingBomb = false;
// Use this for initialization
void Start()
{
bombDropRandomNum = Random.Range(1, 3);
}
void Update()
{
if (!launchingBomb)
{
if (bombDropRandomNum == 1 && bombIsDroped != true)
{
if (planePostion.transform.position.x < -2.75f && planePostion.transform.position.x > -3)
{
dropBomb();
}
}
if (bombDropRandomNum == 2&& bombIsDroped != true)
{
if (planePostion.transform.position.x < -9.5 && planePostion.transform.position.x > -10)
{
StartCoroutine("WaitForSeconds");
StopCoroutine("WaitForSeconds");
}
}
}
void dropBomb()
{
launchingBomb = true;
Instantiate(bomb, gameObject.transform.position, gameObject.transform.rotation);
launchingBomb = false;
}
IEnumerator WaitForSeconds()
{
launchingBomb = true;
dropBomb();
yield return new WaitForSeconds(1);
}
}