代码之家  ›  专栏  ›  技术社区  ›  CubeCrafter360

Unity 2d投下超过1颗炸弹

  •  0
  • CubeCrafter360  · 技术社区  · 7 年前

    当我安装炸弹时,它会制造多个炸弹,有时一次最多制造3个炸弹,我希望它制造1个炸弹,而不是2或3个炸弹。 我需要一种方法来确保它只使用一次真空投弹。 我期待着你的帮助,我想说sry为糟糕的英语,也许这一点,我可能已经错过了一些重要的我的代码

    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 回复  |  直到 7 年前
        1
  •  0
  •   BugFinder    7 年前

    bombdrop函数发射一个新的炸弹,与是否有炸弹已经在行动无关-您需要添加一个条件,即如果炸弹正在发射,则不运行新的炸弹代码。您已经有了bombisdroped变量-如果(!bombisdroped)是新的bombdrop函数,这是有意义的

        2
  •  0
  •   joreldraw    7 年前

    只需在更新中添加一个标志

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