示例#1
0
 // Using "OnTrigger" instead of "OnCollision"
 // because Snakey otherwise got a bit dislocated when eating food.
 private void OnTriggerEnter2D(Collider2D collision)
 {
     // If Snakey eats food.
     if (collision.gameObject.tag == "foodables")
     {
         Destroy(collision.gameObject);
         foods.SpawnFoods();
         bodyPrefab.SetColor(collision.gameObject);
         SpawnBody();
         gameManager.Score += 15;
     }
     // If Snakey touches the outer wall or a spawned block.
     if (collision.gameObject.tag == "gwall" || collision.gameObject.tag == "block")
     {
         // Stop the game from progressing.
         Time.timeScale = 0;
         gameManager.GameOver();
     }
 }