C# 击败所有目标
我有这个脚本,但不起作用,当我杀死一个敌人时,门就会打开,我需要知道当我杀死所有敌人时,门有多活跃。我需要消灭所有的敌人来打开这扇门,但这并不好,如果我杀死一个敌人,这扇门就会无缘无故地打开C# 击败所有目标,c#,unity3d,C#,Unity3d,我有这个脚本,但不起作用,当我杀死一个敌人时,门就会打开,我需要知道当我杀死所有敌人时,门有多活跃。我需要消灭所有的敌人来打开这扇门,但这并不好,如果我杀死一个敌人,这扇门就会无缘无故地打开 public class DoorScript : MonoBehaviour { private GameObject[] enemyToKill; public Transform pos1, pos2; public float speed; public
public class DoorScript : MonoBehaviour
{
private GameObject[] enemyToKill;
public Transform pos1, pos2;
public float speed;
public Transform startPos;
Vector3 nextPos;
public bool EnemyDead = false;
// Start is called before the first frame update
void Start()
{
enemyToKill = GameObject.FindGameObjectsWithTag("EnemyR1");
nextPos = startPos.position;
}
// Update is called once per frame
void Update()
{
foreach (GameObject enemy in enemyToKill)
{
if (enemy == null)
{
EnemyDead = true;
if (transform.position == pos1.position && EnemyDead)
{
nextPos = pos2.position;
}
transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
}
}
}
private void OnDrawGizmos()
{
Gizmos.DrawLine(pos1.position, pos2.position);
}
}
如果我假设正确(上面的代码不清楚),那么您已经为所有敌人设置了一些标记“EnemyR1”,现在您需要检查这些敌人是否存在。如果没有敌人,你需要打开门(比如玩家的出口点)
这也取决于你是否立即杀死敌人(并摧毁游戏对象),或者你只是将敌人标记为某物(设置某些属性/设置为非活动/设置不同的标记)
在要执行的代码中:
void Start()
{
// There you will get ALL objects that have the tag (that was ok before)
// This function always returns Array. Sometimes empty.
enemyToKill = GameObject.FindGameObjectsWithTag("EnemyR1");
nextPos = startPos.position;
}
void Update()
{
// Now it depends how your logic is implemented:
// If you destroy automatically all objects, array will be empty, check length:
if (enemyToKill.Length == 0)
{
//There are no enemies to be killed, You can open the door
}
// Else if objects still exist, you must check if all have the condition.
// They can be disabled, have some property, etc.. In that case You need to:
bool allKilled = true;
foreach (GameObject enemy in enemyToKill)
{
if (enemy.active) //update this condition as you need
{
allKilled = false; // Set to false, when enemy is not killed
break; // and exit the loop
}
}
if (allKilled)
{
//There are no enemies to be killed, You can open the door
}
}
你是在为第一个被杀的敌人设定目标 你应该检查是否所有的敌人都不存在。一般情况下,不要将从
UnityEngine.Object
派生的任何内容与null
进行比较!而是使用隐式bool
运算符。
然后你可以使用(或)像
using System.Linq;
...
void Update()
{
// In general use a bool check!
// A destroyed object reference is not necessarily null
// This returns true if there is still an existing enemy
if(!EnemyDead && enemyToKill.Anyy(enemy => enemy)) return;
// Still keep the flag so the check can be skipped
// As soon as the flag is already set
EnemyDead = true;
if (transform.position == pos1.position)
{
nextPos = pos2.position;
}
transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
}
根据你的代码,我假设当一个游戏对象变为空时,这意味着该对象已死亡。如果是这种情况,您的代码只检查第一次出现的死对象。您需要更改foreach,以便检查您的所有对象是否为空。我使用您的答案,当敌人被杀死时,门不会移动。当我杀死所有敌人时,您放置if(allkill)的行未激活,当我将bool放入时,我会检查此行,并且始终为false。感谢您的回复,问题解决得很好,非常感谢。