Pong – Game Setup

We got stuck on Wednesday, trying to get the walls set up so that the Player objects wouldn’t pass out of view.  The walls also need to be functional so that the ball can bounce off of them.

Working outside of class time, here is the script I came up with which sets up the walls and the player positions:  This script is the GameSetup script, which should be attached to the GameMaster object.

 

using UnityEngine;
using System.Collections;
public class GameSetup : MonoBehaviour {
// We need our camera
 public Camera mainCamera;
// We need the four walls
 public BoxCollider2D topWall;
 public BoxCollider2D bottomWall;
 public BoxCollider2D leftWall;
 public BoxCollider2D rightWall;
// We need the players
 public Transform player1;
 public Transform player2;
// Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {
 // We need the screen's height and width in a Vector3
 // so that we can use the camera's conversion from pixels
 // to units
 Vector3 maxSize = new Vector3 (Screen.width, Screen.height, 0f);
 Vector3 minSize = new Vector3 (0f, 0f, 0f);
// Set up the top wall
 topWall.size = new Vector2 (mainCamera.ScreenToWorldPoint(maxSize).x * 2 , 1f);
 topWall.center = new Vector2 (0f, mainCamera.ScreenToWorldPoint(maxSize).y + .5f);
// Set up the bottom wall
 bottomWall.size = new Vector2 (mainCamera.ScreenToWorldPoint(maxSize).x * 2 , 1f);
 bottomWall.center = new Vector2 (0f, mainCamera.ScreenToWorldPoint(minSize).y - .5f);
leftWall.size = new Vector2 (1f, mainCamera.ScreenToWorldPoint (maxSize).y * 2);
 leftWall.center = new Vector2 (mainCamera.ScreenToWorldPoint (minSize).x - .5f, 0f);
rightWall.size = new Vector2 (1f, mainCamera.ScreenToWorldPoint (maxSize).y * 2);
 rightWall.center = new Vector2 (mainCamera.ScreenToWorldPoint (maxSize).x + .5f, 0f);
// Set the player positions
 Vector3 player1Position = player1.position;
 player1Position.x = mainCamera.ScreenToWorldPoint (new Vector3 (25f, 0f, 0f)).x;
 player1.position = player1Position;
Vector3 player2Position = player2.position;
 player2Position.x = mainCamera.ScreenToWorldPoint (new Vector3 (Screen.width - 25f, 0f, 0f)).x;
 player2.position = player2Position;
 }
}

Today we’ll make the ball bounce, and start in a random direction when the game starts.

 

Here is the script we created attached to the Ball.  This is the BallControl script, and the below scrip will get the ball moving when the game starts.

 

using UnityEngine;
using System.Collections;
public class BallControl : MonoBehaviour {
// Use this for initialization
 void Start () {
 float startX = GetRandomVelocity ();
 float startY = GetRandomVelocity ();
Debug.Log (string.Format ("X: {0} Y: {1}", startX, startY));
rigidbody2D.velocity = new Vector2 (startX, startY);
 }
 
 // Update is called once per frame
 void Update () {
 
 }
private float GetRandomVelocity()
 {
 float returnVal = Random.Range (-15f, 15f);
 if (System.Math.Abs (returnVal) < 5f)
 {
 returnVal = GetRandomVelocity ();
 }
return returnVal;
 }
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *