Unity3D Shooter - 如何在所有敌人被摧毁后切换到下一个级别?
Unity3D Shooter - How to switch to next level after all enemies get destroyed?
我是 Unity 的新手,我正在尝试用 C# 构建一个小型 2d 射击游戏。我现在被卡住了,承认我有点迷茫,不知道最好的方法是什么,但问题是我的英雄向敌人开枪,他们死了,但在敌人都死了之后我如何进入下一个级别?如果我做一个死计数器,我应该输入什么脚本?在敌人脚本中?或者我是否制作了一个新脚本但将其与什么相关联?如果英雄发射了他的六颗子弹(已经有一个计数器可以让英雄在六次射击后不再射击)并且仍然有敌人离开,我还需要游戏结束......
有人给我一些提示吗?谢谢!
敌人脚本:
using System.Collections.Generic;
using UnityEngine;
public class BadguyScript : MonoBehaviour
{
public int maxHealth;
public int curHealth;
private Animator myAnimator;
private bool isDead;
[SerializeField]
private float DespawnTime = 2.5f;
[SerializeField]
private string DeathAnimHash = "isDead";
void Start()
{
myAnimator = GetComponent<Animator>();
myAnimator.enabled =true;
myAnimator.SetBool (DeathAnimHash ,isDead);
maxHealth = 1;
curHealth = maxHealth;
}
void Update()
{
if (curHealth < 1)
{
isDead = true;
myAnimator.SetBool (DeathAnimHash ,isDead);
Destroy(gameObject,DespawnTime);
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (isDead)
return;
if (col.tag == "bullet")
{
curHealth -= 1;
Destroy(col.gameObject);
}
}
}
计算子弹脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameFlow : MonoBehaviour
{
public static float remainingShots = 6;
public Transform shot1;
public Transform shot2;
public Transform shot3;
public Transform shot4;
public Transform shot5;
public Transform shot6;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (remainingShots > 0)
{
shot1.GetComponent<Image> ().enabled = true;
}
else
{
shot1.GetComponent<Image> ().enabled = false;
}
if (remainingShots > 1)
{
shot2.GetComponent<Image> ().enabled = true;
}
else
{
shot2.GetComponent<Image> ().enabled = false;
}
if (remainingShots > 2)
{
shot3.GetComponent<Image> ().enabled = true;
}
else
{
shot3.GetComponent<Image> ().enabled = false;
}
if (remainingShots > 3)
{
shot4.GetComponent<Image> ().enabled = true;
}
else
{
shot4.GetComponent<Image> ().enabled = false;
}
if (remainingShots > 4)
{
shot5.GetComponent<Image> ().enabled = true;
}
else
{
shot5.GetComponent<Image> ().enabled = false;
}
if (remainingShots > 5)
{
shot6.GetComponent<Image> ().enabled = true;
}
else
{
shot6.GetComponent<Image> ().enabled = false;
}
if(Input.GetButtonDown("Fire1"))
{
remainingShots -= 1;
}
}
}
要根据您的条件切换到另一个场景,请执行以下操作:
1. 通过执行以下操作将 OtherScenes 添加到您的游戏中:
File -> Build Settings -> Add Open Scenes
2. 在你的代码中做这样的事情:
Enemy Script.cs
using UnityEngine.SceneManagement; // Contains scene management functions
public GameObject[] enemies;
void Update()
{
enemies = GameObject.FindGameObjectsWithTag("Enemy"); // Checks if enemies are available with tag "Enemy". Note that you should set this to your enemies in the inspector.
if (enemies.length == 0)
{
SceneManager.LoadScene("OtherSceneName"); // Load the scene with name "OtherSceneName"
}
}
Bullet Script.cs
using UnityEngine.SceneManagement;
void Update()
{
if (remainingShots == -1)
{
SceneManager.LoadScene("OtherSceneName");
}
}
使用空 gameobject
并在更新方法中附加这行代码。
if (enemies.length == 0)
{
SceneManager.LoadScene("OtherSceneName"); // Load the scene with name "OtherSceneName"
}
因为你使用的是敌人脚本,所以当所有敌人都死了之后就没有 gameobject
保存脚本了。
我是 Unity 的新手,我正在尝试用 C# 构建一个小型 2d 射击游戏。我现在被卡住了,承认我有点迷茫,不知道最好的方法是什么,但问题是我的英雄向敌人开枪,他们死了,但在敌人都死了之后我如何进入下一个级别?如果我做一个死计数器,我应该输入什么脚本?在敌人脚本中?或者我是否制作了一个新脚本但将其与什么相关联?如果英雄发射了他的六颗子弹(已经有一个计数器可以让英雄在六次射击后不再射击)并且仍然有敌人离开,我还需要游戏结束...... 有人给我一些提示吗?谢谢!
敌人脚本:
using System.Collections.Generic;
using UnityEngine;
public class BadguyScript : MonoBehaviour
{
public int maxHealth;
public int curHealth;
private Animator myAnimator;
private bool isDead;
[SerializeField]
private float DespawnTime = 2.5f;
[SerializeField]
private string DeathAnimHash = "isDead";
void Start()
{
myAnimator = GetComponent<Animator>();
myAnimator.enabled =true;
myAnimator.SetBool (DeathAnimHash ,isDead);
maxHealth = 1;
curHealth = maxHealth;
}
void Update()
{
if (curHealth < 1)
{
isDead = true;
myAnimator.SetBool (DeathAnimHash ,isDead);
Destroy(gameObject,DespawnTime);
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (isDead)
return;
if (col.tag == "bullet")
{
curHealth -= 1;
Destroy(col.gameObject);
}
}
}
计算子弹脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameFlow : MonoBehaviour
{
public static float remainingShots = 6;
public Transform shot1;
public Transform shot2;
public Transform shot3;
public Transform shot4;
public Transform shot5;
public Transform shot6;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (remainingShots > 0)
{
shot1.GetComponent<Image> ().enabled = true;
}
else
{
shot1.GetComponent<Image> ().enabled = false;
}
if (remainingShots > 1)
{
shot2.GetComponent<Image> ().enabled = true;
}
else
{
shot2.GetComponent<Image> ().enabled = false;
}
if (remainingShots > 2)
{
shot3.GetComponent<Image> ().enabled = true;
}
else
{
shot3.GetComponent<Image> ().enabled = false;
}
if (remainingShots > 3)
{
shot4.GetComponent<Image> ().enabled = true;
}
else
{
shot4.GetComponent<Image> ().enabled = false;
}
if (remainingShots > 4)
{
shot5.GetComponent<Image> ().enabled = true;
}
else
{
shot5.GetComponent<Image> ().enabled = false;
}
if (remainingShots > 5)
{
shot6.GetComponent<Image> ().enabled = true;
}
else
{
shot6.GetComponent<Image> ().enabled = false;
}
if(Input.GetButtonDown("Fire1"))
{
remainingShots -= 1;
}
}
}
要根据您的条件切换到另一个场景,请执行以下操作:
1. 通过执行以下操作将 OtherScenes 添加到您的游戏中:
File -> Build Settings -> Add Open Scenes
2. 在你的代码中做这样的事情:
Enemy Script.cs
using UnityEngine.SceneManagement; // Contains scene management functions
public GameObject[] enemies;
void Update()
{
enemies = GameObject.FindGameObjectsWithTag("Enemy"); // Checks if enemies are available with tag "Enemy". Note that you should set this to your enemies in the inspector.
if (enemies.length == 0)
{
SceneManager.LoadScene("OtherSceneName"); // Load the scene with name "OtherSceneName"
}
}
Bullet Script.cs
using UnityEngine.SceneManagement;
void Update()
{
if (remainingShots == -1)
{
SceneManager.LoadScene("OtherSceneName");
}
}
使用空 gameobject
并在更新方法中附加这行代码。
if (enemies.length == 0)
{
SceneManager.LoadScene("OtherSceneName"); // Load the scene with name "OtherSceneName"
}
因为你使用的是敌人脚本,所以当所有敌人都死了之后就没有 gameobject
保存脚本了。