Exemplo n.º 1
0
    // This method is run by Unity whenever the ball hits something. The 'other' parameter
    // contains details about the collision, including the other game object that was hit.
    void OnCollisionEnter2D(Collision2D other)
    {
        // This switch statement compares the other game object name to each of the cases
        // within the switch. If the other game object name matches one of the cases then
        // all the statements underneath that case will be run, until the break statement.
        switch (other.gameObject.name)
        {
        case "Left Wall":
        case "Right Wall":
            directionX = -directionX; // Invert the direction horizontally.
            BounceSound.Play();       // Plays bounce sound
            break;

        case "Top Wall":
        case "Bat":
            directionY = -directionY; // Invert the direction vertically.
            BounceSound.Play();       // Plays bounce sound
            break;

        case "Brick":
        case "Row 2":
        case "Row 3":
        case "Row 4":
        case "Brick (1)":
        case "Brick (2)":
        case "Brick (3)":
        case "Brick (4)":
            directionY = -directionY;     // Invert the direction vertically.
            break;

        case "Goal":
            Bat.Decreaselives(lives);     // Decrease Player live count.
            spawner.DespawnBall(this);    // Despawn this ball.
            break;

        default:
            // If the ball has hit something not listed above, log a console message.
            print(name + " has collided with " + other.gameObject.name);
            break;
        }
    }