Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何阻止两个对象相互重叠?_C#_Unity3d_2d - Fatal编程技术网

C# 如何阻止两个对象相互重叠?

C# 如何阻止两个对象相互重叠?,c#,unity3d,2d,C#,Unity3d,2d,我正在开发一款类似于Flappy Bird和Jetpack Joyride的游戏,我甚至加入了硬币来购买新角色。但是,当一枚硬币产卵时,它会产卵到墙产卵的右侧,这样做是试图阻止硬币在墙后产卵。但是现在,这些墙正在硬币的上面产卵。我怎样才能解决这个问题 生成列的代码: if (!GameController.instance.gamePaused) { timeSinceLastSpawned += Time.deltaTime; if (!GameController.instanc

我正在开发一款类似于Flappy Bird和Jetpack Joyride的游戏,我甚至加入了硬币来购买新角色。但是,当一枚硬币产卵时,它会产卵到墙产卵的右侧,这样做是试图阻止硬币在墙后产卵。但是现在,这些墙正在硬币的上面产卵。我怎样才能解决这个问题

生成列的代码:

if (!GameController.instance.gamePaused)
{
   timeSinceLastSpawned += Time.deltaTime;
   if (!GameController.instance.gameOver && timeSinceLastSpawned >= spawnRate)
   {
       timeSinceLastSpawned = 0f;
       float spawnYPosition = Random.Range(columnMin, columnMax);
       columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
       currentColumn++;
       if (currentColumn >= columnPoolSize)
           currentColumn = 0;
   } //if
} //if
if(!GameController.instance.gamePaused)
{
    if (!GameController.instance.gameOver)
    {
        timeSinceLastSpawned += Time.deltaTime;

        if (timeSinceLastSpawned>=spawnRate)
        {
            int randCoin = Random.Range(0, 99);
            int coinRand = coinRarity[randCoin];
            switch(coinRand)
            {
                case 1:
                    timeSinceLastSpawned = 0f;
                    float goldSpawnYPosition = Random.Range(coinMin, coinMax);
                    goldCoins[goldCurrentColumn].transform.position = new Vector2(spawnXPosition, goldSpawnYPosition);
                    goldCurrentColumn++;
                    if (goldCurrentColumn >= goldPoolSize)
                        goldCurrentColumn = 0;
                    break;
                case 2:
                    timeSinceLastSpawned = 0f;
                    float diamondSpawnYPosition = Random.Range(coinMin, coinMax);
                    diamondCoins[diamondCurrentColumn].transform.position = new Vector2(spawnXPosition, diamondSpawnYPosition);
                    diamondCurrentColumn++;
                    if (diamondCurrentColumn >= diamondPoolSize)
                        diamondCurrentColumn = 0;
                    break;
                case 3:
                    timeSinceLastSpawned = 0f;
                    float rubySpawnYPosition = Random.Range(coinMin, coinMax);
                    rubyCoins[rubyCurrentColumn].transform.position = new Vector2(spawnXPosition, rubySpawnYPosition);
                    rubyCurrentColumn++;
                    if (rubyCurrentColumn >= rubyPoolSize)
                        rubyCurrentColumn = 0;
                    break;
                case 4:
                    timeSinceLastSpawned = 0f;
                    float emeraldSpawnYPosition = Random.Range(coinMin, coinMax);
                    emeraldCoins[emeraldCurrentColumn].transform.position = new Vector2(spawnXPosition, emeraldSpawnYPosition);
                    emeraldCurrentColumn++;
                    if (emeraldCurrentColumn >= emeraldPoolSize)
                        emeraldCurrentColumn = 0;
                    break;
            } //switch
        } //if
    } //if
} //if
硬币产卵代码:

if (!GameController.instance.gamePaused)
{
   timeSinceLastSpawned += Time.deltaTime;
   if (!GameController.instance.gameOver && timeSinceLastSpawned >= spawnRate)
   {
       timeSinceLastSpawned = 0f;
       float spawnYPosition = Random.Range(columnMin, columnMax);
       columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
       currentColumn++;
       if (currentColumn >= columnPoolSize)
           currentColumn = 0;
   } //if
} //if
if(!GameController.instance.gamePaused)
{
    if (!GameController.instance.gameOver)
    {
        timeSinceLastSpawned += Time.deltaTime;

        if (timeSinceLastSpawned>=spawnRate)
        {
            int randCoin = Random.Range(0, 99);
            int coinRand = coinRarity[randCoin];
            switch(coinRand)
            {
                case 1:
                    timeSinceLastSpawned = 0f;
                    float goldSpawnYPosition = Random.Range(coinMin, coinMax);
                    goldCoins[goldCurrentColumn].transform.position = new Vector2(spawnXPosition, goldSpawnYPosition);
                    goldCurrentColumn++;
                    if (goldCurrentColumn >= goldPoolSize)
                        goldCurrentColumn = 0;
                    break;
                case 2:
                    timeSinceLastSpawned = 0f;
                    float diamondSpawnYPosition = Random.Range(coinMin, coinMax);
                    diamondCoins[diamondCurrentColumn].transform.position = new Vector2(spawnXPosition, diamondSpawnYPosition);
                    diamondCurrentColumn++;
                    if (diamondCurrentColumn >= diamondPoolSize)
                        diamondCurrentColumn = 0;
                    break;
                case 3:
                    timeSinceLastSpawned = 0f;
                    float rubySpawnYPosition = Random.Range(coinMin, coinMax);
                    rubyCoins[rubyCurrentColumn].transform.position = new Vector2(spawnXPosition, rubySpawnYPosition);
                    rubyCurrentColumn++;
                    if (rubyCurrentColumn >= rubyPoolSize)
                        rubyCurrentColumn = 0;
                    break;
                case 4:
                    timeSinceLastSpawned = 0f;
                    float emeraldSpawnYPosition = Random.Range(coinMin, coinMax);
                    emeraldCoins[emeraldCurrentColumn].transform.position = new Vector2(spawnXPosition, emeraldSpawnYPosition);
                    emeraldCurrentColumn++;
                    if (emeraldCurrentColumn >= emeraldPoolSize)
                        emeraldCurrentColumn = 0;
                    break;
            } //switch
        } //if
    } //if
} //if
当一个墙在一枚硬币上生成时,我希望发生的是让它这样硬币被“删除”(在本例中,只是从墙中的位置移动到屏幕外的原始生成点,因为它全部在一个硬币“池”中)
非常感谢您的帮助。

这可以通过使用
碰撞器来完成,或者在您的情况下,可能是
碰撞R2D。
我猜您已经在使用碰撞器来检测玩家是否击中硬币或墙壁,所以只需添加逻辑来检测碰撞对象是柱体还是玩家

void OnTriggerEnter2D(Collider2D col)
{
    if(col.name == "playerObject"){
        //Increase coins?
    }
    this.respawn();
}

这可能是我没有意识到的一个非常明显的修正。但无论如何,谢谢你向我指出这一点me@DodgeCoder如果您想要一个不太明显的解决方案,您还可以通过向子对象添加碰撞器来挑战自己,给它一个特定的层,然后调整物理矩阵,以便只有列层将触发该子对象的
OnTriggerEnter
!(别忘了确保玩家对象不会击中该层!)