Unity Engine is a game development engine aimed at making games easier to develop. For many beginners, especially those without coding experience, the idea of creating a video game can seem like a daunting task, but with a development engine like Unity that process can be made much easier.
Steps
Part 1
Part 1 of 10:
Creating the Project
Part 2
Part 2 of 10:
Exploring the Unity Editor
-
1Review the left side. This is the current scene you're looking at as well as what game objects are in the scene. Currently the scene is named "Untitled" and the only object in it is the "Main Camera". This area is also referred to as the "Hierarchy".
-
2Click on the Camera in the Hierarchy to select it. This will highlight it in blue.
-
3Find the inspector. The right side of the screen is the inspector, and this shows you the properties of game objects you're selecting. The "main camera" is currently selected, hence the fact that it's highlighted in blue in the hierarchy on the left, so the inspector shows the properties of the main camera.
-
4Review the asset folder and console, at the bottom. This is where all of the assets in the game (cameras, scenes, scripts, etc.) are stored. This is also where you can import files to use in unity.
- Click on the "Project" tab to ensure the asset folder is open if it isn't already.
-
5Find the scene view at the center of the screen. This shows you what is currently in the scene, and you can toggle between the scene view and the game view using the buttons the arrows are pointing to.
- Right above the scene view there are the buttons that allow you to play the scene and pause the scene to see what it would look like for a normal player.
-
6Find the manipulator buttons. Finally, in the upper left corner you can see different options that allow you to manipulate the scene, and objects in it in different ways.Advertisement
Part 3
Part 3 of 10:
Creating Player 1
-
1Create Player 1. To start, download the sprite.
-
2Import the sprite into the asset folder. Find where the image has been saved on your computer, and drag it from that folder into the asset folder inside of the Unity Editor.
-
3Right click inside of the hierarchy and go to 2D Object, and create a Sprite.
- Ensure the created sprite isn't a child of the Main Camera. If there's a drop down arrow next to the camera you've accidentally make the sprite a child of the main camera. Try to ensure that nothing in the hierarchy is like this.
- If it is a child simply grab the Sprite and drag it down a bit inside of the hierarchy. This will unchild it.
-
4Click on the object we've just created you can see information about it on the right side. This area is called the inspector, and this is where you can modify some things about the object. Firstly rename it to "Player 1".
-
5Set the position of the object to (0, 0, 0). Sometimes objects will start with transform values that may place them off screen, so be sure to check that when creating new objects.
-
6Apply the sprite to the Sprite Renderer of on Player 1. Click on player 1 in the scene, and drag the sprite from the asset folder to the "sprite" box on the Sprite Render component in the inspector.
-
7Add a Box Collider 2D to the Paddle. Click "Add Component" and search for "Box Collider 2D", Make sure this is the 2D version , and not simply the Box Collider.
-
8Add a Rigidbody 2D using the same process. Click "Add Component" and search for "Rigidbody 2D". Now in the inspector we're going to change some properties of the Rigidbody.
-
9Change the "Gravity Scale" to 0. This ensures the paddle won't be affected by gravity.
-
10Click the "Constraints" drop down menu, and then check "Freeze Position" for the x value, and "Freeze Rotation" for the z value. This ensures the Paddle will only move in the Y axis, or simply will only move up and down.Advertisement
Part 4
Part 4 of 10:
Writing the Paddle Code
-
1Create the script that controls the Paddles behavior. Right click in the Asset menu at the bottom, and go to Create > C# Script . Name the script "Paddle" so it's easy to keep track of it.
-
2Double click on the newly created script to open it.
- Inside of the C# Script you should have a blank project.
-
3Type code above the Start() function that declares the up and down arrows, and how to move the player.
- The up and down are keys that you will set later to move the paddle up and down. Rigidbody2D is what you modify to allow the player to move.
- When you type new code a yellow bar will appear on the side. This shows what code was recently added to the script, and will go away once you save the script.
public KeyCode up ; public KeyCode down ; Rigidbody2D rigidBody ;
-
4Tell the Rigidbody variable to find the "Rigidbody" that was attached to the paddle earlier. Type
rigidBody = GetComponent<Rigidbody2D>();
inside of the start function. -
5Type the following into the update function.
- This will tell the paddle how it's supposed to move when you press up or down. Basically, if the player presses the "up" button they will move up 7 units per second, if they press "down" they will move down 7 units per second, and if they press nothing they wont move.
if ( Input . GetKey ( up )) { rigidBody . velocity = new Vector2 ( 0f , 7f ); } else if ( Input . GetKey ( down )) { rigidBody . velocity = new Vector2 ( 0f , - 7f ); } else { rigidBody . velocity = new Vector2 ( 0f , 0f ); }
-
6Press Ctrl + S to save the script,. Head back into the Unity Editor by either tabbing back, or closing Visual Studio.Advertisement
Part 5
Part 5 of 10:
Creating Player 2
-
1Select the Player 1 Game Object in the scene by clicking on it inside of the hierarchy.
-
2Apply the Player script to the Player 1 Game Object. Click "Add component" on Player 1, and search for the name of the player script. In this case the name is "Paddle".
- You could also click and drag the script from the asset menu to the Inspector on the right.
-
3Select the Player 1 paddle. Under the "Paddle" component in the inspector, there should be a drop down menu to select up or down. Choose the keys you wish to make the player move up or down. For this example the "W" and "S" keys are used.
-
4Move the player more towards the left side of the screen. Click on Player 1 in the scene, and change the X value in the position to -8.
-
5Create Player 2 by making the current player into what is called a Prefab. To do this you're going to select it in the scene, and drag it down to the asset panel. This will create the prefab.
-
6Drag that prefab back into the scene. Now you've made a clone of Player 1, except this clone can take on different values if you want it to.
-
7Rename the new object to "Player 2". Change its x value to a positive 8, and set the keys to move this object in the same way you did previously, this time using the up and down arrows.
-
8Press the play button at the top. You can see the game run, and you have two different objects that can move with different keys.Advertisement
Part 6
Part 6 of 10:
Creating the Play Area
-
1Right click on the scene. Click on "Create Empty" now add a Box Collider 2D to the object.
-
2Move the object so it's more towards the top of the screen. Select the move tool in the upper left.
-
3Click on the green arrow on the game object. Drag it towards the top to create the upper wall.
-
4Click "Edit Collider" to change the boundaries so they cover all the area from the left and right paddle.
- When you click off of the Wall in the hierarchy, the outline for the green wall will disappear, but don't worry, it's still there; it just doesn't display it unless it's selected.
-
5Right click on the top wall in the hierarchy, and click duplicate. Then drag it down so underneath the paddles, so that it serves as the bottom wall.
-
6Check the result. This is how it should look.Advertisement
Part 7
Part 7 of 10:
Creating the Ball
-
1Create the ball that you'll hit back and forth. Download the sprite for the ball.
-
2Import the downloaded sprite into the Unity Editor.
-
3Right click in the hierarchy, and create a sprite. Rename this sprite to "Ball".
-
4Apply the imported sprite to the game object.
-
5Add a Circle Collider 2D, and a Rigidbody 2D. Remember to turn the gravity scale to 0, and angular drag to 0, and finally set the rotation in the z axis to be locked.
-
6Create a physics material for the ball. This is what will allow it to bounce off the walls. Right click in the asset folder, and go to "Create Physics Material 2D"
-
7Name the material something like "Bounce." Set the friction to 0 and the bounciness to 1. This will ensure it never loses velocity.
-
8Apply the material to the Rigid body of the ball game object.Advertisement
Part 8
Part 8 of 10:
Creating the Ball Code
-
1Right click in the asset folder, and go to Create > C# Script . Name the script "Ball". Now double click on that script to open it.
-
2Type
Rigidbody2D rigidBody;
above the start function, andrigidBody = GetComponent<Rigidbody2D>();
inside the Start() function.- This gives us access to the balls Rigidbody, which will allow us to apply force to it.
-
3Create a function that will set the ball'ss velocity. Type the following underneath the "Start()" and "Update()" functions.
public void Direction () { }
-
4Type the following inside of the newly created Direction() function:
- This is what the function Random.Range will do for you, by generating a random number either 0 or 1. Then it will give the ball a velocity of either (5, -3) or (-5, 3) depending.
int direction ; direction = Random . Range ( 0 , 2 ); if ( direction == 0 ) { rigidBody . velocity = new Vector2 ( 5f , - 3f ); } else { rigidBody . velocity = new Vector2 (- 5f , 3f ); }
-
5Add
Direction()
to the balls "Start()" function. This will cause it to trigger when the game starts. -
6Press Ctrl + S to save the script. Head back to the Unity Editor.
-
7Apply the Ball script to the Ball game object. Select the ball in the hierarchy, and then drag the Ball script from the assets folder onto the Ball in the inspector.Advertisement
Part 9
Part 9 of 10:
Creating the Goals
-
1Right click, Create > Empty , and apply a Box Collider 2D to the object you just created. Set them up a bit behind the paddles, and ensure that they cover from the top wall to the bottom wall on both sides
-
2Check the "Is Trigger" box under "Box Collider 2D". This allows us to have something happen when an object enters that trigger. In this case it will reset the ball to the center.Advertisement
Part 10
Part 10 of 10:
Creating the Goal Code
-
1Right click in the asset folder, and click Create > C# Script . Rename the script to "Goal" Double click on the newly created script to open it. .
-
2Type the following under the Start() function:
void OnTriggerEnter2D ( Collider2D collider ) { collider . GetComponent < Ball >(). Direction (); collider . transform . position = new Vector2 ( 0f , 0f ); }
- "collider.transform.position = new Vector2(0f, 0f);" is how the ball gets set back to the initial position. The collider in this case being the ball.
- "collider.GetComponent<Ball>().Direction();" gets the Direction function on the ball when it passes through, and makes sure it runs again.
- So essentially the ball returns to the center and once again chooses a random direction.
-
3Press Ctrl + S to save the script. Then head back to the Unity Editor.
-
4Attach the script to both goals by using "Add component" in the inspector.
-
5Press Play and witness the finished project.
-
6Go to File > Save project so that you can keep your project forever.Advertisement
Expert Q&A
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
About this article
Thanks to all authors for creating a page that has been read 13,652 times.
Advertisement