Unity – pong game.. why not

org-pong

The original pong game was not much compared to what would come later. For many it was amazing that something like was available for use in the home.  I have seen others make pong games in Unity and thought it might be fun to try. Pictures of the console show controls for two players. For this project I’ll make the second player the computer. Yeah it will hard to bet but….

Using Unity 5.5 start out with a basic camera.

unity-pong-1

 

Using a graphic tool I made a paddle and a ball(png format). Create a folder named Sprites and drop in the two images. Create a empty game object named ‘player’

unity-pong-2

Drag the paddle sprite onto the player game object and notice the Sprite Renderer show up as a component of the player.  Select the player object and then Select Component->Physics->RigidBody from the menu. The paddle will need this in order to bounce the ball.

unity pong 3.png

Create  a new folder named Physics. Select Asset->Create->PhysicsMaterial. Name it bounce. When applied to the paddle will cause the ball to bounce back.

unity-pong-4

In order for the paddle to react to the ball hitting it we need to add a collider component. A BoxCollider will do. Set the material of the collider to the bounce material.

unity-pong-5

Create a new game object called Ball and add in the ball sprite. Add a RigidBody and collider as well.

unity pong 6.png

So far there is one paddle and a ball. Not so good?

There needs to be some code in order to make this work. Create a new folder named Scripts and add a new c# file named paddle.cs. Below is what the code should look like( or close).

The Update function is part of the core Unity MonoBehavoir class. It is called once per frame. The variable ‘gameObject’ refers to the object to which this script is attached.

Input.GetAxis(“Vertical”) will return  -1 or 1 depending if the down or up arrow key is pressed.  This value times the speed will be used to increment the position of the game object.

playerPosition = new Vector2(-20,Mathf.Clamp(yPosition,-13,13));

This line creates a new 2d Vector with a fixed X location  of -20(where I placed the paddle on the screen), and a new value of Y based on the new yPosition. Mathf.Clamp() restricts the y value to between -13 and 13. These values were determined by experimentation.

The last line transforms the object to a new position. Since the x value is always -20 the paddle will only move up or down.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class paddle : MonoBehaviour {
 public float speed=3;
 public float yPosition;
 public Vector2 playerPosition;
 // Update is called once per frame
 void Update () {
    yPosition=gameObject.transform.position.y+(Input.GetAxis("Vertical")*speed);
    playerPosition=new Vector2(-20,Mathf.Clamp(yPosition,-13,13));
    gameObject.transform.position =playerPosition;
 }
}

 

 

 

 

About gricker

Living and learning
This entry was posted in Uncategorized. Bookmark the permalink.