Domanda

There is a remake of the Snake game. The snake's head is a trigger, and the apple uses only a collider. Now OnTriggerEnter() does not work every time - the snake's trigger have to enter several times into apple's body to get it.

There is code used for eating:

void Head.OnTriggerEnter(Collider col)
{
    if(col.CompareTag("Food"))
    {
        gameController.FoodEated();
    }
}

public void GameController FoodEated()
{
    Destroy(currentFood);
    InitializeMeal();
    head.GrowUp();
}

void GameController.InitializeMeal()
{
    currentFood = (GameObject)Instantiate(foodPrefab, FindFreeSpace(), Quaternion.identity);
}

And there is some kind of magic: The first apple could be eaten normally, but the snake can walk through the second apple a few times before the apple will be activated.

I tried every trigger functions and none of them are working. http://www.youtube.com/watch?v=z_UQi7SGOLw - video of a bug. The snake is going through the apple for 3-4 frame updates. I have another idea for realizing an apple, but a trigger is more appropriate, I think.

È stato utile?

Soluzione

If your snake moves too fast and the apple collider is too small than I guess that Unity's engine can sometimes miss collisions.

If on the snake you have a Rigidbody component than you can increase collision detection quality by changing "Collision Detection" from "Discrete" to "Continues" or "Continues Dynamic".

Altri suggerimenti

As you can see on the video above, my snake is growing by creating a tailelement at the current transform.position. TailElement starts moving when the distance between last two tail elements is more or equals 1.

I just tried to decrease the radius of the head's collider by 10% and now the tail element is not collider during the movement of snake. Now it works fine.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top