Engine Time!
Unity Pong
Meet Unity, the engine behind Hollow Knight and Cuphead-scale indies: scenes, GameObjects, components and your first C# scripts, assembled into Pong with real physics.
Meet Unity, the engine behind Hollow Knight and Cuphead-scale indies: scenes, GameObjects, components and your first C# scripts, assembled into Pong with real physics.
Set up Unity and understand its core idea: everything is components.
Write a C# MonoBehaviour that moves the paddle.
using UnityEngine;
public class PaddleMovement : MonoBehaviour
{
public float speed = 8f;
public KeyCode upKey = KeyCode.W;
public KeyCode downKey = KeyCode.S;
void Update()
{
float move = 0f;
if (Input.GetKey(upKey)) move = 1f;
if (Input.GetKey(downKey)) move = -1f;
Vector3 pos = transform.position;
pos.y += move * speed * Time.deltaTime;
pos.y = Mathf.Clamp(pos.y, -3.6f, 3.6f);
transform.position = pos;
}
}
transform.position = pos;pos.y += move * speed * Time.deltaTime;Vector3 pos = transform.position;pos.y = Mathf.Clamp(pos.y, -3.6f, 3.6f);pos.y += move * speed * Time.; pos.y = Mathf.(pos.y, -3.6f, );
Let Unity’s physics engine drive the ball, no movement code at all.
using UnityEngine;
public class BallLauncher : MonoBehaviour
{
public float speed = 6f;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
Launch();
}
public void Launch()
{
float dirX = Random.value < 0.5f ? -1f : 1f;
float dirY = Random.Range(-0.6f, 0.6f);
rb.linearVelocity = new Vector2(dirX, dirY).normalized * speed;
}
}
Detect goals with trigger zones and keep score.
using UnityEngine;
public class GoalZone : MonoBehaviour
{
public GameManager manager;
public bool isLeftGoal;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Ball"))
{
manager.ScorePoint(isLeftGoal);
other.transform.position = Vector3.zero;
other.GetComponent<BallLauncher>().Launch();
}
}
}
void (Collider2D other)
{
if (other.("Ball"))
manager.ScorePoint(isLeftGoal);
}
Draw the score on screen and end the match at 7.
using UnityEngine;
using TMPro;
public class GameManager : MonoBehaviour
{
public TMP_Text leftText, rightText, winnerText;
int leftScore, rightScore;
public void ScorePoint(bool leftGoalWasHit)
{
if (leftGoalWasHit) rightScore++;
else leftScore++;
leftText.text = leftScore.ToString();
rightText.text = rightScore.ToString();
if (leftScore == 7 || rightScore == 7)
{
winnerText.text = (leftScore == 7 ? "LEFT" : "RIGHT")
+ " PLAYER WINS!";
winnerText.gameObject.SetActive(true);
Time.timeScale = 0f; // freeze the game
}
}
}
Add the classic flawed AI and BUILD your game as a real application.
using UnityEngine;
public class AIPaddle : MonoBehaviour
{
public Transform ball;
public float speed = 5f; // slower than the player!
public float deadZone = 0.4f;
void Update()
{
float diff = ball.position.y - transform.position.y;
if (Mathf.Abs(diff) > deadZone)
{
Vector3 pos = transform.position;
pos.y += Mathf.Sign(diff) * speed * Time.deltaTime;
pos.y = Mathf.Clamp(pos.y, -3.6f, 3.6f);
transform.position = pos;
}
}
}
if (Mathf.(diff) > deadZone)
pos.y += Mathf.(diff) * speed * Time.deltaTime;
Keep the Unity cheatsheet handy
MonoBehaviour, physics and UI in one place - use it beside this workshop.
Give your Unity Pong a look!
Use Pixel Studio to design a unique paddle and ball - make your Unity Pong stand out.