我是 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. 通过执行以下操作将其他场景添加到您的游戏中:
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");
}
}
关于c# - Unity3D 射击游戏 - 所有敌人被消灭后如何切换到下一关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58489666/