This tutorial enhances a basic Pong clone built in Unity, adding classic Pong aesthetics, improved gameplay mechanics, and a retro-styled UI. Let's upgrade our Pong game!
Part 2: Retro Revamp & Enhanced Gameplay
This tutorial builds on the previous Pong clone. Download the project from GitHub or complete Part 1 before proceeding. A working demo and the final improved game are also available.
Key Improvements:
BallController
script's OnCollisionEnter2D
method to use the launchAngle
function. Remember to tag the Player and Enemy paddles appropriately.EnemyController
script to use InvokeRepeating("Move", .02F, .02F)
, adjusting speed and timing for optimal smoothness. Add bounds to prevent the AI paddle from going off-screen. The BoundsController
script should reset the enemy paddle's position after the ball is destroyed.UIManager
script manages pausing and resuming, along with showing/hiding pause menu elements. Make sure to tag the pause menu elements with "ShowOnPause".AutoPlayer
and AutoEnemy
scripts (similar to the enemy AI but with adjustments for speed and timing) to control the AI paddles in the main menu.PointCounter
script), and ends the game when a score limit (e.g., 7 points) is reached, displaying a game over screen. The GameOver
script displays the winner. Remember to tag the game over elements with "ShowOnFinish".Code Snippets:
(BallController.cs - OnCollisionEnter2D & launchAngle)
void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == "Enemy") { float y = launchAngle(transform.position, col.transform.position, col.collider.bounds.size.y); Vector2 d = new Vector2(1, y).normalized; rig2D.velocity = d * speed * 1.5F; } if (col.gameObject.tag == "Player") { float y = launchAngle(transform.position, col.transform.position, col.collider.bounds.size.y); Vector2 d = new Vector2(-1, y).normalized; rig2D.velocity = d * speed * 1.5F; } } float launchAngle(Vector2 ball, Vector2 paddle, float paddleHeight) { return (ball.y - paddle.y) / paddleHeight; }
(EnemyController.cs - Move)
void Move() { if (ball == null) { ball = GameObject.FindGameObjectWithTag("Ball").transform; } ballRig2D = ball.GetComponent<Rigidbody2D>(); if (ballRig2D.velocity.x > 0) { if (ball.position.y > this.transform.position.y + .5F) { transform.Translate(Vector3.up * speed * Time.deltaTime); } else if (ball.position.y < this.transform.position.y - .5F) { transform.Translate(Vector3.down * speed * Time.deltaTime); } } // ... bounds checking ... }
(UIManager.cs - Partial)
// ... other methods ... public void showFinished() { foreach (GameObject g in finishObjects) { g.SetActive(true); } } // ... other methods ...
(PointCounter.cs)
void Update () { text.text = rightBound.GetComponent<BoundController>().enemyScore + "\t\t" + leftBound.GetComponent<BoundController>().playerScore; }
(GameOver.cs)
void Update () { if(uiManager.playerWon){ text.text = "GAME OVER!\nPLAYER WON!"; } else if(uiManager.enemyWon){ text.text = "GAME OVER!\nENEMY WON!"; } }
Remember to adjust values like speed and timing in the AI scripts to fine-tune the gameplay difficulty. This enhanced Pong clone provides a more complete and engaging retro gaming experience. The GitHub link (not provided in the input) would contain the full project files.
The above is the detailed content of Building a Pong Clone in Unity: UI and Gameplay. For more information, please follow other related articles on the PHP Chinese website!