Пример #1
0
    /*<summary>
     * OnTriggerEnter checks with wihich object head is colliding.
     * When collision is detected, AudioManager is calling to play sound.
     * </summary>
     */
    private void OnTriggerEnter(Collider other)
    {
        Debug.Log(other.gameObject.name);

        //first tail element. Head can collide with it because of specificaton of tail movement.
        //It does not crush the game, because it is not posible to bump into first tail element,
        //because head can not be turned 180 degrees
        if (other.gameObject == GameController.snake[1])
        {
            return;
        }

        if (other.gameObject.tag == "Wall" || other.gameObject.tag == "Tail")
        {
            //play sound
            AudioManager.instance.PlaySound("GameOver");

            //gameover
            GameManager.instance.GameOver();

            return;
        }

        if (other.gameObject.tag == "NormalFood")
        {
            //playSound
            AudioManager.instance.PlaySound("NormalFood");

            //add points
            GameObject.FindObjectOfType <UIController>().AddPoints(1);

            //instantiate new food
            other.gameObject.GetComponent <Food>().AddNewOne();
        }

        if (other.gameObject.tag == "SpecialFood")
        {
            //play sound
            AudioManager.instance.PlaySound("SpecialFood");

            //add points
            GameObject.FindObjectOfType <UIController>().AddPoints(10);
        }

        //destroy existing
        Destroy(other.gameObject);

        //add snake tail
        SnakeTail tail = GameController.snake[GameController.snake.Count - 1] as SnakeTail;

        tail.AddElement(GameController.snake);
    }