Pong Continued

When last we met, we put some of the rudimentary elements of our Pong game into Unity.  We used Paint.NET to create our background, ball, and player images.  We added them to Unity, and added some movement scripts to our players.  We made it so that we could assign the movement controls to each player, rather than hard-code the keys into the scripts.

Here are the scripts involved:

PlayerControl (attached to the Player object)

using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
public KeyCode upKey;
 public KeyCode downKey;
public float movementSpeed = 10f;
// Update is called once per frame
 void Update () {
 // Create our new Vector
 Vector2 playerVector = rigidbody2D.velocity;
 if (Input.GetKey (upKey)) // The user pushed the up eky
 {
 playerVector = new Vector2(0f, movementSpeed);
 } else if (Input.GetKey (downKey)) // The user pushed the down key
 {
 playerVector = new Vector2(0f, movementSpeed * -1 );
 } else // The user didn't push either key
 {
 playerVector = new Vector2(0f, 0f);
 }
 
 // Apply the new vector to the player
 rigidbody2D.velocity = playerVector;
 }
}

Today, we’re going to work on the Game Setup script, which will be attached to our Game Manager.  The Game Setup script will size and position the walls, relative to the constraints of the camera.  It will also position the players, also appropriate to the constraints of the camera view.

Leave a Reply

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