Steffan's Game Development Portfolio

See Projects Go to git repositories
Pong 2016
November 2015 - January 2016

Pong 2016 is the first game I worked on during my education as Game Developer at ROC Friese poort, after the introduction period in which we had to follow courses from all the IT directions.
The assignment for pong was to make a remake of the well known game pong. The game had to contain more than one ball and 3d physics. To add some extra challenge for myself I decided to add a one-player mode with an enemy AI. The game was built for a mobile platform like android or iOS

External Links
Pong 2016

Game WebGL build
The game was made for mobile devices, so I recommend playing it on a mobile device for an optimal experience.



Paddle Control Script - User Input
The Paddle Control script hadles user input and transfers it to the paddle that is controlled by that user, it hadles input from a keyboard and touch input for mobile devices

PaddleControl.cs
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PaddleControl : MonoBehaviour
  5. {
  6.  
  7.     //Enum, to make the setting idiot proof
  8.     public enum DirectionType { x, z };
  9.     //Defines on which axis the paddle moves
  10.     public DirectionType direction;
  11.  
  12.     //Keys for movement on the axes
  13.     public KeyCode keyPlus;
  14.     public KeyCode keyMin;
  15.  
  16.     //Limits for movement on the axes
  17.     public float limitPlus;
  18.     public float limitMin;
  19.  
  20.     //Paddle speed
  21.     public float speed = 0.5F;
  22.  
  23.     //bool for ai checks
  24.     private bool ai;
  25.  
  26.     // Update is called once per frame
  27.     void Update()
  28.     {
  29.         //Check if ai is enabled on this paddle, if false there will be no controls affecting this paddle
  30.         if (!ai)
  31.         {
  32.             //KeyInput controls
  33.             if (Input.GetKey(keyPlus))
  34.             {
  35.                 if (direction == DirectionType.x)
  36.                 {
  37.                     if (transform.position.x < limitPlus)
  38.                     {
  39.                         transform.position = new Vector3(transform.position.x + speed * Time.deltaTime, transform.position.y, transform.position.z);
  40.                     }
  41.                 }
  42.                 else
  43.                 {
  44.                     if (transform.position.z < limitPlus)
  45.                     {
  46.                         transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + speed * Time.deltaTime);
  47.                     }
  48.                 }
  49.             }
  50.             if (Input.GetKey(keyMin))
  51.             {
  52.                 if (direction == DirectionType.x)
  53.                 {
  54.                     if (transform.position.x > limitMin)
  55.                     {
  56.                         transform.position = new Vector3(transform.position.x - speed * Time.deltaTime, transform.position.y, transform.position.z);
  57.                     }
  58.                 }
  59.                 else
  60.                 {
  61.                     if (transform.position.z > limitMin)
  62.                     {
  63.                         transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - speed * Time.deltaTime);
  64.                     }
  65.                 }
  66.             }
  67.             //touch controls
  68.             foreach (Touch touch in Input.touches)
  69.             {
  70.                 //half of the screen width and height
  71.                 double HalfScreenw = Screen.width / 2.0;
  72.                 double HalfScreenh = Screen.height / 2.0;
  73.                 //check on wich side of the screen the paddle is
  74.                 if (Camera.main.WorldToScreenPoint(transform.position).x > HalfScreenw && touch.position.x > HalfScreenw)
  75.                 {
  76.                     //move the paddle according to touchposition
  77.                     if (touch.position.y > HalfScreenh)
  78.                     {
  79.                         if (transform.position.z < limitPlus)
  80.                         {
  81.                             transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + speed * Time.deltaTime);
  82.                         }
  83.                     }
  84.                     else
  85.                     {
  86.                         if (transform.position.z > limitMin)
  87.                         {
  88.                             transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - speed * Time.deltaTime);
  89.                         }
  90.                     }
  91.                 }
  92.                 else if (Camera.main.WorldToScreenPoint(transform.position).x < HalfScreenw && touch.position.x < HalfScreenw)
  93.                 {
  94.                     //move paddle according to touch position
  95.                     if (touch.position.y > HalfScreenh)
  96.                     {
  97.                         if (transform.position.z < limitPlus)
  98.                         {
  99.                             transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + speed * Time.deltaTime);
  100.                         }
  101.                     }
  102.                     else
  103.                     {
  104.                         if (transform.position.z > limitMin)
  105.                         {
  106.                             transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - speed * Time.deltaTime);
  107.                         }
  108.                     }
  109.                 }
  110.             }
  111.         }
  112.     }
  113.  
  114.     //functions to access private field from other classes
  115.     public void setAI(bool ai) {
  116.         this.ai = ai;
  117.     }
  118. }
PaddleControl.cs


Paddle AI Script
The Paddle AI script controls a paddle by using the location data of all the pong balls in the scene, picks the closest one and moves towards and reflects it to the player. It uses some values from the paddle control script, but this was before I knew about inheritance so instead of inheriting those values it accesses it through a constructor variable.

PaddleAI.cs
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PaddleAI : MonoBehaviour {
  5.     //The ai script of the paddle
  6.  
  7.     //stores the main control script of the paddle
  8.     PaddleControl pc;
  9.  
  10.     //Speed based on difficulty
  11.     public float easySpeed = 7.5F;
  12.     public float averageSpeed = 17.5F;
  13.     public float hardSpeed = 30.0F;
  14.  
  15.     //the speed of the ai
  16.     private float speed;
  17.  
  18.     //this var tells if the ai is enabled or not
  19.     private bool ai;
  20.  
  21.  
  22.     // Use this for initialization
  23. 	void Start () {
  24.         //Inherit ai variable from the game controller
  25.         ai = GameController.Instance.ai;
  26.         //Get the main paddle control and store it as a var for easy access
  27.         pc = GetComponent<PaddleControl>();
  28.         //Send ai settings to paddlecontrol
  29.         pc.setAI(ai);
  30.  
  31.         //set ai speed according to dificulty
  32.         if (GameController.Instance.diff == GameController.Difficulty.Easy)
  33.         {
  34.             speed = easySpeed;
  35.         }
  36.         else if (GameController.Instance.diff == GameController.Difficulty.Average)
  37.         {
  38.             speed = averageSpeed;
  39.         }
  40.         else {
  41.             speed = hardSpeed;
  42.         }
  43.     }
  44.  
  45.     // Update is called once per frame
  46.     void Update() {
  47.         //if ai is enabled
  48.         if (ai) {
  49.             //get all balls
  50.             GameObject[] balls = GameObject.FindGameObjectsWithTag("Ball");
  51.             GameObject closestBall = null;
  52.             //find closest ball
  53.             foreach (GameObject ball in balls) {
  54.                 if (closestBall != null)
  55.                 {
  56.                     if (Vector3.Distance(transform.position, ball.transform.position) < Vector3.Distance(transform.position, closestBall.transform.position)) {
  57.                         if (!ball.GetComponent<Ball>().GetInCollider()) {
  58.                             closestBall = ball;
  59.                         }
  60.                     }
  61.                 }
  62.                 else {
  63.                     closestBall = ball;
  64.                 }
  65.             }
  66.  
  67.             //move towards closest ball according to paddle control movement settings
  68.             if (pc.direction == PaddleControl.DirectionType.x)
  69.             {
  70.                 int dir = 0;
  71.                 if (closestBall.transform.position.x < transform.position.x - 1 && closestBall.transform.position.x < transform.position.x + 1)
  72.                 {
  73.                     dir = -1;
  74.                 }
  75.                 else if (closestBall.transform.position.x > transform.position.x - 1 && closestBall.transform.position.x > transform.position.x + 1)
  76.                 {
  77.                     dir = 1;
  78.                 }
  79.                 else
  80.                 {
  81.                     dir = 0;
  82.                 }
  83.                 if (dir == 1 && transform.position.x < pc.limitPlus)
  84.                 {
  85.                     transform.position = new Vector3(transform.position.x + speed * Time.deltaTime, transform.position.y, transform.position.z);
  86.                 }
  87.                 else if (dir == -1 && transform.position.x > pc.limitMin)
  88.                 {
  89.                     transform.position = new Vector3(transform.position.x - speed * Time.deltaTime, transform.position.y, transform.position.z);
  90.                 }
  91.             }
  92.             else {
  93.                 int dir = 0;
  94.                 if (closestBall.transform.position.z < transform.position.z - 1 && closestBall.transform.position.z < transform.position.z + 1)
  95.                 {
  96.                     dir = -1;
  97.                 }
  98.                 else if (closestBall.transform.position.z > transform.position.z - 1 && closestBall.transform.position.z > transform.position.z + 1)
  99.                 {
  100.                     dir = 1;
  101.                 }
  102.                 else
  103.                 {
  104.                     dir = 0;
  105.                 }
  106.                 if (dir == 1 && transform.position.z < pc.limitPlus)
  107.                 {
  108.                     transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + speed * Time.deltaTime);
  109.                 }
  110.                 else if (dir == -1 && transform.position.z > pc.limitMin)
  111.                 {
  112.                     transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - speed * Time.deltaTime);
  113.                 }
  114.             }
  115.         }
  116.     }
  117. }
  118.  
PaddleAI.cs