Block Break: Tuning

I added in a few additional modules into the GameSession code so the game now has 3 balls per play and it displays the total in the top left. I also made a “tilt” feature that is triggered by the space bar on hte Ball object that may help in the event the ball does get stuck.

Added to GameSession:
[SerializeField] TextMeshProUGUI ballText;
[SerializeField] int ballCount = 3;

Added to GameSession Start:
ballText.text = "Balls: " + ballCount.ToString();

Added to GameSession:
    public int AdjustBallCount(int Caller)
    {
        if (Caller == 1)
        {
            ballCount--;
        }
        else if (Caller == 2)
        {
            ballCount++;
        }

        ballText.text = "Balls: " + ballCount.ToString();
        return ballCount;
    }

Added to LoseCollider:
        int ballCount = FindObjectOfType<GameSession>().AdjustBallCount(1);
        
        if (ballCount == 0)
        {
            SceneManager.LoadScene("Game Over");
        }
        else
        {
            gameBall.hasStarted = false;
            gameBall.Update();
        }

Added to Ball Update:
if(Input.GetKeyDown(KeyCode.Space))
        {
            TiltBall();
        }

Added to Ball:
public void TiltBall()
    {
        tiltCount--;

        if (tiltCount > 0)
        {
            Vector2 originalVelocity = new Vector2
                (myRigidBody2d.transform.position.x, 
                myRigidBody2d.transform.position.y);

            Vector2 velocityBump = new Vector2
                (myRigidBody2d.transform.position.x,
                Random.Range(0f, randomFactor));

            myRigidBody2d.velocity += velocityBump;
            myRigidBody2d.velocity = originalVelocity;
        }
        
    }

I think the tilt count be better executed, but it works for the time being. Would like to add to the tiltCount after a certain period of time.

I also altered where the pointsPerBlockDestroyed was housed. Given that larger blocks that take more damage to destroy should be worth more, I pushed that field over to the Block script. I added a call to the AddToScore module of the GameSession type, passing the destroyed block’s value.

Added to Block :
[SerializeField] int pointsPerBlockDestroyed = 5;

Added to Block DestroyBlock:
FindObjectOfType<GameSession>().AddToScore(pointsPerBlockDestroyed);

Updated GameSession AddToScore:
    public void AddToScore(int pointsPerBlockDestroyed)
    {
        currentScore += pointsPerBlockDestroyed;
        scoreText.text = currentScore.ToString();
    }
2 Likes

Hey there. Im impressed. i’ve been trying to do something simialr, but i don’t seem to be able to spawn the new ball on the paddle after it goes through the lose collider. How did you manage this? I’ve been trying to pull the paddle position and place the new ball at that but it does not seem to work

1 Like

I wanted to make a “lives” system too, so I had the same problem. The way I resolve it was to create a new function that encapsulated everything that was in the “Start” function of the “Ball” script, and then run it again everytime a new life had to be spawned.

Privacy & Terms