我有一个生成无限关卡的游戏,实例中总是有3个关卡,问题是这些关卡中有移动的敌人,有时玩家过慢或过快,并且敌人处于他们不应该(例如掉进玩家并立即杀死他)。
我认为的解决方案是冻结每个添加的块(最后一个int当前实例块的列表),并且当玩家进入下一个块时,将其解冻,因此敌人一开始就开始移动进入块,而不是生成块。
以下是冻结和解冻的功能:
public void UnfreezeCurrentLevelBlockEnemies()
{
if(currentBlocks.Count == 3)
{
foreach (Rigidbody2D enemy in currentBlocks[1].transform)
{
enemy.constraints = RigidbodyConstraints2D.None;
Debug.Log("I'm in index 1 levelblock freezing" );
}
} else
{
Debug.LogWarning("Generating initial blocks, blocks will not be
frozen yet...");
}
}
public void FreezeLastLevelBlockEnemies()
{
if (currentBlocks.Count == 3)
{
foreach (Rigidbody2D enemy in currentBlocks[2].transform)
{
enemy.constraints = RigidbodyConstraints2D.FreezeAll;
Debug.Log("I'm in index 2 levelblock freezing" );
}
} else
{
Debug.LogWarning("Generating initial blocks, blocks will not be
frozen yet...");
}
}
如何冻结所有敌人并适当地解冻它们?
我想冻结敌人(鸟和老鼠),这是关卡的结构:
游戏视频,以便您更好地理解问题:https://www.youtube.com/watch?v=lXzOoEbo0Aw&feature=youtu.be
答案 0 :(得分:0)
解决了它,以防万一有人遇到我的问题,这是如何冻结敌人:
public void FreezeLevelBlockEnemies(int index)
{
if (currentBlocks.Count == 3 && freezeEnemiesOutOfCurrentBlock)
{
foreach (MouseMovement enemy in currentBlocks[index].GetComponentsInChildren<MouseMovement>())
{
enemy.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
Debug.Log("I'm in " + index + " index levelblock freezing");
}
foreach (BirdMovement enemy in currentBlocks[index].GetComponentsInChildren<BirdMovement>())
{
enemy.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
Debug.Log("I'm in " + index + " index levelblock freezing");
}
} else
{
Debug.LogWarning("Freeze is disabled");
}
}