Hullo! :)
One of the subscribers of the Programmer's Ranch Facebook page requested an article on creating Arkanoid with Unity. So... here you go. :)
Create a new Unity project and save your scene. Set up your scene using scaled up cubes for the scene boundaries (except for the bottom), and put in another cube for the paddle and a sphere for the ball:
Make sure that all objects have the same z-coordinate for position, and that they are visible to the camera.
The game mechanics for Arkanoid are very similar to those of Pong, so we will be following very similar steps. As a matter of fact, we can use the same Ball script we used in Pong. Create a new C# script, attach it to the sphere, and after opening it in MonoDevelop, paste the code we used in Pong:
public class Ball : MonoBehaviour
{
private Vector3 direction;
private float speed;
// Use this for initialization
void Start ()
{
this.direction = new Vector3(1.0f, 1.0f).normalized;
this.speed = 0.1f;
}
// Update is called once per frame
void Update ()
{
this.transform.position += direction * speed;
}
}
Attach a Rigidbody to the sphere via Component menu -> Physics -> Rigidbody, which will allow the ball to bounce off surfaces. Remember to turn off "Use Gravity".
Next, select the Main Camera and change Projection from Perspective to Orthographic to remove perspective and end up with a more suitable 2D view. Change the Size property until the scene fits the camera's view properly:
Create a new C# script and name it Paddle. Attach it to the cube to be used as a paddle. Paste the following code to enable left and right movement via arrow keys:
public class Paddle : MonoBehaviour
{
private float speed = 0.1f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey(KeyCode.LeftArrow))
this.transform.position += Vector3.left * speed;
if (Input.GetKey(KeyCode.RightArrow))
this.transform.position += Vector3.right * speed;
}
}
This is quite similar to what we did in the Pong article, except we're moving left and right instead of up and down. We are using the static vectors Vector3.left and Vector3.right, which are really just shortcuts for unit vectors along the particular directional axis (in this case (-1, 0, 0) and (1, 0, 0), respectively). We similarly used Vector3.up ((0, 1, 0)) and Vector3.down ((0, -1, 0)) in Pong.
We can now take care of the bricks. Since we will have a lot of them, it's best to create a prefab. From the GameObject menu, select Create Other -> Cube, and drag it into the Project panel in Unity to make a prefab out of it. Rename it to Brick. Create a new tag called Brick and tag your brick as such. Remember to click the Apply button in the inspector to make the tag apply to all instances of the prefab.
Proceed to duplicate (Ctrl+D) the brick so that there are many bricks in the top part of the screen:
To make the ball destroy bricks as it hits them, add the following code at the end of the OnCollisionEnter() method in the Ball script:
if (collision.gameObject.tag == "Brick")
GameObject.Destroy(collision.gameObject);
You can now play happily as the ball destroys bricks but not the paddle or the edges:
That wraps up the basic game mechanics for Arkanoid. As an exercise, add victory (for when you clear all the bricks) and defeat (for when the ball escapes below the paddle) conditions.
In these recent tutorials about classic arcade games, I have purposely focused exclusively on game mechanics (e.g. bouncing, shooting, etc). This was intentional. When making a game, you should first concentrate on making solid game mechanics that work perfectly. Then, you can focus on building the rest of the game (levels, menus, screens between levels, etc) and polishing (colours, animation, etc).
In fact, this Arkanoid game looks like shit. The first thing I'd do after finishing the game mechanics is give it a touch of colour (via materials, and put in a point light to brighten the colours):
...and here's what it looks like in-game:
This colouring took a couple of minutes to do, and I think you'll agree with me when I say that the game looks much more alive. Making the game vibrant and fun is just as important as programming the game mechanics, so don't overlook it!
Thanks for reading, and I hope you found this useful! :)
Showing posts with label reflection. Show all posts
Showing posts with label reflection. Show all posts
Monday, June 10, 2013
Friday, June 7, 2013
Unity3D: Pong
Hi folks! :)
Today we're going to see how to create a game like Pong, one of the earliest classic arcade video games, using Unity3D. The approach for movement is very similar to the one we used in "C# Threading: Bouncing Ball", although we don't have to worry about threads in this case.
After creating a new project, use the GameObject -> Create Other menu to set up the scene using four cubes and a sphere. After scaling the cubes to be elongated, put one on each side: at the left and right as paddles, and at the top and bottom as walls that the ball can then bounce on:
One thing you'll notice is that the paddles don't look like straight lines - they are seen as if from the side. That's because we're using a perspective camera. For a game like Pong, where we don't care about perspective, an orthographic camera is probably better.
Select the Main Camera from the Hierarchy panel, and change the Projection property from Perspective to Orthographic. Change the Size property until the game objects fit the camera's view comfortably (check this by pressing Play in Unity, not from the camera preview, since the resolution is different):
Now, let's make the ball move. Right click in the Project panel and select Create -> C# Script. Name it Ball and drag it onto the Sphere. Double-click the script to open it in MonoDevelop.
Just like in "C# Threading: Bouncing Ball", we now give the ball a direction and a speed with which to move (Physics people will know they are together called velocity):
public class Ball : MonoBehaviour
{
private Vector3 direction;
private float speed;
// Use this for initialization
void Start ()
{
this.direction = new Vector3(1.0f, 1.0f).normalized;
this.speed = 0.1f;
}
// Update is called once per frame
void Update ()
{
this.transform.position += direction * speed;
}
}
The normalized part is something we do for convenience. A normalised vector has a magnitude of 1, making it easy to work consistently with it. We then modify the speed using the speed variable.
In order to implement the bounce effect, we have the ball detect collisions, and change trajectory when a collision occurs:
void OnCollisionEnter(Collision collision)
{
Vector3 normal = collision.contacts[0].normal;
direction = Vector3.Reflect(direction, normal);
}
Take a look at this diagram to understand what's happening here:
In order to make the ball bounce, we need to find where it hits a wall or paddle, and change its direction. We reflect the direction in the surface normal, which is at 90 degrees to the surface itself. In Unity, we get that surface normal from the point of contact with the surface, which is available from collision.contacts.
Although it is good to know the vector mathematics behind vector reflection, in Unity3D this is as easy as using Vector3.Reflect() and passing in the incident direction and the surface normal.
In order for this to work. You will need to add a rigidbody to the Sphere, via Component menu -> Physics -> Rigidbody.
Now, just add another script for the paddle movement, and attach it to the paddles:
public class Paddle : MonoBehaviour
{
private float speed = 0.1f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey(KeyCode.UpArrow))
this.transform.position += Vector3.up * speed;
if (Input.GetKey(KeyCode.DownArrow))
this.transform.position += Vector3.down * speed;
}
}
Enjoy the game!
In this article, we created a simple Pong game and learned about orthographic vs perspective cameras as well as vector reflection (used to bounce an object off a wall).
This simple game mechanic can be used for various classic games including Pong and Arkanoid.
The only thing we didn't do is handle when the ball leaves the area, in which case the player should lose the game. I'll leave that as an exercise since it's quite trivial (you can do something like how we handle bullets in "Unity3D: Space Invaders (Part 4 - Collisions)", and when the ball leaves the area, you change to a different scene showing a loser's screen - see "Unity3D: Scenes and Building").
In the next article, we will revisit the bouncing ball theme and make an Arkanoid clone. So check back! :)
Today we're going to see how to create a game like Pong, one of the earliest classic arcade video games, using Unity3D. The approach for movement is very similar to the one we used in "C# Threading: Bouncing Ball", although we don't have to worry about threads in this case.
After creating a new project, use the GameObject -> Create Other menu to set up the scene using four cubes and a sphere. After scaling the cubes to be elongated, put one on each side: at the left and right as paddles, and at the top and bottom as walls that the ball can then bounce on:
One thing you'll notice is that the paddles don't look like straight lines - they are seen as if from the side. That's because we're using a perspective camera. For a game like Pong, where we don't care about perspective, an orthographic camera is probably better.
Select the Main Camera from the Hierarchy panel, and change the Projection property from Perspective to Orthographic. Change the Size property until the game objects fit the camera's view comfortably (check this by pressing Play in Unity, not from the camera preview, since the resolution is different):
Now, let's make the ball move. Right click in the Project panel and select Create -> C# Script. Name it Ball and drag it onto the Sphere. Double-click the script to open it in MonoDevelop.
Just like in "C# Threading: Bouncing Ball", we now give the ball a direction and a speed with which to move (Physics people will know they are together called velocity):
public class Ball : MonoBehaviour
{
private Vector3 direction;
private float speed;
// Use this for initialization
void Start ()
{
this.direction = new Vector3(1.0f, 1.0f).normalized;
this.speed = 0.1f;
}
// Update is called once per frame
void Update ()
{
this.transform.position += direction * speed;
}
}
The normalized part is something we do for convenience. A normalised vector has a magnitude of 1, making it easy to work consistently with it. We then modify the speed using the speed variable.
In order to implement the bounce effect, we have the ball detect collisions, and change trajectory when a collision occurs:
void OnCollisionEnter(Collision collision)
{
Vector3 normal = collision.contacts[0].normal;
direction = Vector3.Reflect(direction, normal);
}
Take a look at this diagram to understand what's happening here:
In order to make the ball bounce, we need to find where it hits a wall or paddle, and change its direction. We reflect the direction in the surface normal, which is at 90 degrees to the surface itself. In Unity, we get that surface normal from the point of contact with the surface, which is available from collision.contacts.
Although it is good to know the vector mathematics behind vector reflection, in Unity3D this is as easy as using Vector3.Reflect() and passing in the incident direction and the surface normal.
In order for this to work. You will need to add a rigidbody to the Sphere, via Component menu -> Physics -> Rigidbody.
Now, just add another script for the paddle movement, and attach it to the paddles:
public class Paddle : MonoBehaviour
{
private float speed = 0.1f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey(KeyCode.UpArrow))
this.transform.position += Vector3.up * speed;
if (Input.GetKey(KeyCode.DownArrow))
this.transform.position += Vector3.down * speed;
}
}
Enjoy the game!
In this article, we created a simple Pong game and learned about orthographic vs perspective cameras as well as vector reflection (used to bounce an object off a wall).
This simple game mechanic can be used for various classic games including Pong and Arkanoid.
The only thing we didn't do is handle when the ball leaves the area, in which case the player should lose the game. I'll leave that as an exercise since it's quite trivial (you can do something like how we handle bullets in "Unity3D: Space Invaders (Part 4 - Collisions)", and when the ball leaves the area, you change to a different scene showing a loser's screen - see "Unity3D: Scenes and Building").
In the next article, we will revisit the bouncing ball theme and make an Arkanoid clone. So check back! :)
Subscribe to:
Posts (Atom)









