Unity 2d下降超过1枚炸弹

时间:2017-10-11 09:27:08

标签: c# unity3d 2d

当我制造炸弹的时候它会产生更多,有时甚至达到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);
    }
}

2 个答案:

答案 0 :(得分:0)

爆炸功能会发射一枚与炸弹已经在行动无关的新炸弹 - 你需要添加一个条件,如果炸弹射击不能运行新的炸弹代码。你已经拥有了bombisdroped变量 - 如果(!bombisdroped)那么你的新炸弹功能是有意义的

答案 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);
    }
}