Esempio n. 1
0
    public void handleBallHit(Collision2D collision, Rigidbody2D rb)
    {
        //Collision with paddle
        if (collision.collider == paddleLeft || collision.collider == paddleRight)
        {
            titleMusic.pitch += 0.005f;
            ball.speed        = ball.speed + (float)0.1;
            float y = transform.position.y - collision.transform.position.y;
            Debug.Log(y);

            float x = 0;

            if (collision.collider == paddleLeft)
            {
                player1.pingpong.Play(0);
                x = 1;
                setLastPlayer(true);
            }
            else
            {
                player2.pingpong.Play(0);
                x = -1;
                setLastPlayer(false);
            }

            Vector2 dir = new Vector2(x, y).normalized;
            rb.velocity = dir * ball.speed;
        }
        //Collision with wall
        if (collision.collider == wallLeft || collision.collider == wallRight)
        {
            goal.playSound();
            titleMusic.pitch = 1;
            ball.speed       = 10;
            if (collision.collider == wallRight)
            {
                ball.initBallRight = true;
                increaseScore(true);
            }
            else
            {
                ball.initBallRight = false;
                increaseScore(false);
            }
            ball.resetBall();
        }
    }