【问题标题】:Freezing enemies and unfreezing them in a Infinity Runner game在 Infinity Runner 游戏中冻结敌人并解冻他们
【发布时间】:2019-05-07 19:26:38
【问题描述】:

我有一个游戏可以生成无限的关卡块,并且总是实例化 3 个块,问题是这些关卡块有移动的敌人,有时玩家太慢或太快,而敌人的位置他们不应该这样(比如掉入玩家并立即杀死他)。

我认为的解决方案是冻结每个添加的块(当前实例块列表中的最后一个),当玩家进入下一个块时,解冻它,这样敌人就会在玩家开始移动进入区块,而不是在它生成的时候。

下面是冻结和解冻的函数:

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

【问题讨论】:

    标签: c# unity3d game-physics


    【解决方案1】:

    解决了,以防万一有人遇到我的问题是如何冻结敌人:

    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");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-30
      • 2013-12-15
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多