11) Building the game + Organisation code

Here is a screencast where I show how to build the game and where I also show some methods to add some organisation to your code and inspector.

I forgot to show how a variable can be hidden using [HideInInspector] so here is an image showcasing that. The note text variable is not visible in the inspector.

unityblog_11_img1

The game that we made is very simplistic but features could still be added to it. There are many different places to look for Unity tutorials and other game engines to try if you want. This is but a small step on your journey through game development.

 

10) Extending the game

In this post we are going to layout some extra steps in order to make creating more levels easier, we are also going to create a menu for starting the game.

Menu

Create a new scene and save it as Menu, we are going to create a canvas for UI elements, create a button and set the pos y to -40, change the text in the text child of the button to say Start. Add the LevelLoad script to the button add the event to the button in the same manner as the last post, click the plus to add an event and drag the button into the space, look for the LoadLevel function and change the number to 1.

Add the Menu scene to the scenes in build but make sure that the menu is at index 0, the scene at index 0 is the scene that will be loaded when someone opens the built game, we will be building the game in another post.

unityblog_10_img1

We are now going to edit the camera object, change the clear flags to single color. This will change the background to a single color.

unityblog_10_img2

The final step for the menu is to create a Title text, create a text object ensuring that it’s a child of the canvas. Set the text color to black, font size to 40 and the horizontal and vertical overflow to overflow. Set the pos y of the text to 30. This is how the menu should look,

unityblog_10_img3

If you press play and click the start button, you will be sent to Level_1. Save the Menu scene and open Level_1.

Prefabs

If we were to add more levels to the game it would involve have to individually create all of the components again(except the scripts and materials). There is a way to store game objects in the project folder for reuse in any scene, that is a prefab. Create a folder for Prefabs called Prefabs.

Let’s start with the player, drag the player object to the Prefabs folder, we now have the player as a prefab which we can use in later levels. You will also notice that the player object name in the hierarchy has turned blue.

unityblog_10_img5

All of the references to objects in the scene will be removed from the player prefab when it is made into a prefab.

unityblog_10_img4

Drag the wall, key, respawn point, waypoint, enemy, key, door, ground and victory point objects down into the prefabs folder as well. Drag the canvas into the prefabs folder, this will make all of the children of the canvas prefabs as well.

If we are going to have multiple levels, then we need a way to access the next level when we complete the level. Duplicate the restart button in the scene and rename it Next Level Button. Change the text in the child to Next Level. This button should function like the other button but the idea is that you would add the number for the next level in there. Change the pos y of the restart button to 40.

We are going to make a slight change to the Victory script in order to make theĀ  next level button appear when we complete the level.

unityblog_10_img6

This will allow the next level button to appear when the level is completed which in turn will allow you to move to the next level.

Applying changes to prefabs

We added this button to the canvas but it is not part of the prefab, at least not yet. What we are going to do is click apply on the canvas object in the hierarchy.

unityblog_10_img7

This will apply any changes made on a prefab in the scene to the prefab data as changes in scene are not applied automatically. Be sure to add the reference to the next level button in the victory point object in the scene and also make sure that the LoadLevel event on the restart button matches the number of the current level in the build settings.

With all of these extra features set up we can now create more levels easily. Here I have another quick level as an example of how you can just drag and drop prefabs into the scene in order to create a level.

unityblog_10_img8

Be sure to build the lightmap and the navigation mesh. You may have noticed in the UI post that another object called the event system was created as well. This is need to manage interaction with UI but when we brought our canvas prefab in an event system wasn’t created so go to Create -> UI -> Eventsystem. Add the camera controller to the camera and set up the reference to our player.

The camera position still need to be changed from the default, we are going to copy the values from the first level. Save Level_2 and open Level_1, go to the camera and click the gear at the transform, select copy component then open Level_2 and select the gear on the camera in that scene and select paste component values.

Make sure to add Level_2 to the build settings and set the next level button in Level_1 to 2. You can add more levels if you so choose but the point is that making new levels is significantly easier with the use of prefabs. For the last post of the tutorial, we will talk about building the game and showing some extra coding features.

8) Winning and Losing the Game

Currently our enemy and player can interact with each other but we haven’t set a consequence of what happens if the player loses all of their lives. We also do not currently have a win condition so in this post we are going to add both of those features.

Create a cube and call it Victory Point, change the scale to 2, 0.1, 2 and change the position to -7, 0, 18.5. We are going to create a new material and call it Goal. Set this color to green.

unityblog_8_img1

Add the material to the victory panel and set the box collider to a trigger collider.

unityblog_8_img2

Next we are going to create a script called Victory. Continue reading “8) Winning and Losing the Game”

7) Enemy Interaction

In this post we are going to add some interactivity between the enemy and the player. First we are going to add a sphere collider to the enemy. Select isTrigger and set the radius to 0.8.

unityblog_7_img1

Our next step is to create an empty game object called Respawn Point. Set it’s position to 0, 0.5, 0. The plan here is that when the enemy collides with the player, the player will lose a life and be sent back to the respawn point and lose a life.

Open up the PlayerStats script, Continue reading “7) Enemy Interaction”

5) Keys and Doors

In this post we are going to create keys for the player to collect in order to open a door which will lead to the exit.

First we will create a new material and name it “Key”, give the material a yellow color. Unlike the previous two materials we are going to increase the metallic slider on the material, increase it to about 0.9.

unityblog_5_img1

This can simply change the look of the material, this will make our keys more distinct. Next we will create a sphere in our scene, click and drag the material on to the sphere in order to give it the new material, this will act as our key in the game.

unityblog_5_img2

Rename the sphere to Key. Continue reading “5) Keys and Doors”

4) Creating the Level & Colliders

In this post we are going to create the level of the game and I will also talk about colliders in Unity.

Creating the level

In the video below I will create the level in Unity using cubes and adjusting the size, you do not have to follow what I did exactly but it should give you an idea of how I went about creating the level.

Continue reading “4) Creating the Level & Colliders”

2) Moving the Player

In this post we are going to create a script that will allow the player to move around in the scene. First we are going to make some adjustments to the scene and characters. First change the camera xĀ rotation to 70 and change the zĀ positionĀ to -3.UnityBlog_2_img1

Next change the scale x and scale z of the plane to 10, rename the plane object to “Ground”. Once you’ve done that, rename the cube to player.

We are also going to give the player a tag called player, click on the player either in the scene or in the list of objects in the hierarchy, directly underneath the object name there’s a tag section, click on it and select the Player tag. This should be an available tag from the list of tags, you are able to create your own tags but we won’t be doing that for the moment.

We are going to add a new component to the player which will help it move around the scene. Continue reading “2) Moving the Player”