References


Used chatgpt to help me understand how to store and use transform.position as the Unity docs confused me abit.

https://chat.openai.com/share/96798fef-f4f1-4bfe-b3f4-f5c6b0944074

font used:

https://www.fontspace.com/ro-twimch-font-f83247

sound used:

https://creatorassets.com/a/8-bit-powerup-sound-effects

How the environment was set up:
I ended up only using one sprite that I made in paint.net and just scaling it to make the ball and the walls. The ball was given a rigidbody2d, box collider and a physics material with the friction set to 0 and the bounce to 1. Walls just had box colliders, and each player had a polygon collider and a rigidbody2d.

How the scoring and game logic works:

Within the ball script, the position is constantly checked within FixedUpdate(), if the ball exceeds the position of player 2, then the ball is reset using a method within the ball script, and Player1Scored() method is called from the ScoreKeeper script and vice versa.

The ScoreKeeper script has private score Ints within it, everytime each of the playerScored() methods are called, the score is incremented by one. There is a FixedUpdate() within the ScoreKeeper script that checks if either of the scores are equal to 10. If that happens, the game is paused using Time.timeScale = 0, and the OpenScene method (taken from the EndGameListener script within the tutorials) is called for the end of the game scene.

At the end of game scene, there's a button that contains a script that is just like the SceneSwitcher, except there is a Time.timeScale = 1 statement at the start of the method so there are no time issues.

How Player 1, 2, and the AI player are made:

The movements for player 1 and 2 are just taken from the PlayerMovement script from the tutorials. A public bool(isPlayerOne) was made within the PaddleMovement script and toggled on player one in the unity editor to tell the script if it was controlling player one or two.

To toggle the second player between being controlled by a player and AI, a PlayerPrefs script "GameMode" was added to the menu buttons. 

How it would work is that if the "singleplayer" button was activated, the PlayerPrefs key "gameMode" was set to 0. Then the If statement within the PlayerMovement script would check "If (PlayerPrefs.GetInt("gameMode") == 1))", if that was true, then the AI would be activated and the player 2 movements would be disabled. The else statement would just be the player 2 controls.

The AI is just comparing its current Y value to the ball, if the Y value was less than the ball's, then it would transform up and vice versa.

How the sound was implemented:
This helpful video explains most of it, On collision, the ball script checks if the tag contains "Player", if so then the PaddleHit sound is activated, else plays the wall sound (Because what else could it hit within the game?). The ScoreKeeper script has a play sound within each of its score methods, and within the game over system in the ScoreKeeper script.

The ball script:
I haven't really explained the ball scripts "reset" function yet so ill do that now. Inside of the ball script, theres a method called BallMovement(), which contains the variables with values below:

collisionMultiplier = 0f;

transform.position = Vector3.zero;

rb.velocity = Vector3.zero;

I only would've known to assign the last two variables with Vector3.zero because of chatgpt (yes shame on me ';-;). What these variables do is just basically resetting the collisionMultiplier, transform.position, and rb.velocity.

        float angle = Mathf.Deg2Rad * Random.Range(45, 135);

        if (Random.Range(0, 100) > 50) angle *= -1;

        rb.AddForce(new Vector2(speedToMove * Mathf.Sin(angle), speedToMove * Mathf.Cos(angle)));

The first two lines were taken from Lake (idk if she got it somewhere else before) within the discord, and the last line was taken from the StartMovingInRandomDirection within the tutorials. Without getting these two sources I would've been completely lost because my head just hurts trying to figure it out.

This chunk of code just sets it so the ball faces a range that isnt straight up or down and adds force to it. (its probably alot more complicated than that but thats all I really get from it).

the collisionMultiplier is incremented by .1 on each collision, and adds a force that is the current velocity of the ball scaled by the collisionMultiplier.

Files

WebGL.zip Play in browser
Mar 17, 2024