玩家死亡时,统一C#射击游戏停止克隆

时间:2018-12-03 17:45:02

标签: c# unity3d

所以我正在和一个奶奶射击鸭子一起做游戏。我奶奶快死了 她的生命结束后,但我正试图让鸭子停止克隆,直到她 死了,所以比赛结束了,有人吗?我对此非常了解,因此不胜感激。如果需要还可以提供更多代码

public class GrannyController : MonoBehaviour
{

    public float speed;
    public int health = 10;
    public Text healthDisplay;

    private Rigidbody2D rb;
    private Vector2 moveVelocity;
    public int score;
    public GameObject gameOverPanel;


    public bool IsAlive()
    {
        return this.health > 0;
    }

    public void Die()
    {
        //StartCoroutine(DieCoroutine);
        Destroy(this.gameObject);
    }


    // Use this for initialization
    void Start()
    {
        score = 0;
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        healthDisplay.text = "Lives :" + health;

        if (health <= 0)
        {
            //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
            GameOver();
        }

        {
            Vector2 moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
            moveVelocity = moveInput.normalized * speed;
        }
    }
    void FixedUpdate()

    {
        rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
    }

    void GameOver()
    {
        CancelInvoke();
        gameOverPanel.SetActive(true);
    }
}

public class SpawnerController : MonoBehaviour
{

    public GrannyController player;
    public GameObject duck;
    public Transform[] spawnSpots;
    private float timeBtwSpawns;
    public float startTimeBtwSpawns;

    // Use this for initialization
    void Start()
    {
        timeBtwSpawns = startTimeBtwSpawns;
    }

    // Update is called once per frame
    void Update()
    {
        if (!player.IsAlive()) return; // Check if player is alive, stop if not

        timeBtwSpawns -= Time.deltaTime;

        if (timeBtwSpawns <= 0)
        {
            int randPos = Random.Range(0, spawnSpots.Length);
            Instantiate(duck, spawnSpots[randPos].position, Quaternion.identity);
            timeBtwSpawns = startTimeBtwSpawns;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您只需要使用布尔值或状态即可,而不必创建新的实例。

在SpawnerController中可能是这样的,例如创建一个名为spawneable的布尔值,该布尔值可以是公共的,您可以将其从玩家死亡更改为false,从而停止克隆:

void Update()
{

    if (timeBtwSpawns <= 0)
    {
        if(spawneable)
        {
           int randPos = Random.Range(0, spawnSpots.Length);
           Instantiate(duck, spawnSpots[randPos].position, Quaternion.identity);
           timeBtwSpawns = startTimeBtwSpawns;
        }
    }
    else
    {
        timeBtwSpawns -= Time.deltaTime;
    }

}

这是我能想到的最简单的方法,不是最好的方法,但是您只需几行就可以了。

答案 1 :(得分:0)

在GrannyController脚本中创建方法IsAlive(),该方法返回有效/无效(真/假)。

public bool IsAlive()
{
    return this.health > 0;
}

这是更新的Spawner。请记住,将带有GrannyController脚本的游戏对象分配给SpawnerController对象。

public class SpawnerController : MonoBehaviour
{

    public GrannyController player;
    public GameObject duck;
    public Transform[] spawnSpots;
    private float timeBtwSpawns;
    public float startTimeBtwSpawns;

    // Use this for initialization
    void Start()
    {
        timeBtwSpawns = startTimeBtwSpawns;
    }

    // Update is called once per frame
    void Update()
    {
        if(!player.IsAlive()) return; // Check if player is alive, stop if not

        timeBtwSpawns -= Time.deltaTime;

        if (timeBtwSpawns <= 0)
        {
            int randPos = Random.Range(0, spawnSpots.Length);
            Instantiate(duck, spawnSpots[randPos].position, Quaternion.identity);
            timeBtwSpawns = startTimeBtwSpawns;
        }
    }
}