Using Unity 5.3, we are going to create a 3D runner game that we can then play on a mobile device. The basic concept for this game is to have a character that can move left to right, and obstacles that are moving towards the character which he must avoid.
After you have created your project, you’ll be staring at an empty world. Lets start by right clicking in the hierarchy on the left and adding a plane to our scene.
Now select the plane and on the right side in the inspector change the Scale of the Z value to 10 so our plane becomes long and narrow.
Now we need our actual 3D character, if you already have a 3D model you can use that, but we will just be using the default one Unity gives us.
Lets start by right clicking in the bottom where the Assets are at and go to Import Package -> Characters and then select Import on the next window.
Over in our Assets we can now drill down from Standard Assets -> Characters -> ThirdPersonCharacter -> Prefabs and we should see a ThirdPersonController, drag and drop that controller onto the scene.
Now that we have our character, lets get him into the right position, I set his X, Y, and Z to 0, and rotated him on his Y axis by 180 degrees so he’s facing the right way.
We also need to adjust the existing Main Camera so that it is behind the character and facing the same way.
So now if we select the Game tab at the top, we will see how our game will actually look from the camera’s perspective!
This game is easy!
Now lets add some obstacles for our character to avoid. We will use a basic cube, so to add one go to the top tool bar and select GameObject -> 3D Object -> Cube.
Once the cube has been placed, lets move it down the plane, and make it taller by scaling the Y axis.
This is what our scene should look like.
Get Things Moving
We have one obstacle in our game now, but it’s not moving yet. What we need to do now is create a script that will handle moving all of our obstacles. Start by right clicking in the Assets and go to Create -> C# Script, I named mine MovingObstacles. Now we need to assign this script to an object, you can choose to create an empty object, but I’m going to assign this script to the plane. So select the plane and then in the Inspector we need to click Add Component -> Scripts -> MovingObstacles.
Since this script is assigned to one object in our scene, it will only have one running instance during gameplay. In the Assets window, double click the script so we can look at its code. There should be two empty methods, a start and update, the update method will get called once per frame.
Adding a Tag
In order to obtain an instance of our cube in the script, we need to assign a tag to the cube. Start by selecting the cube and going to the Inspector, you’ll notice that the Tag is currently Untagged. If we select that, we have the option to add a tag.
I’m going to add a new tag called Cube1.
Select your cube again and make sure that it’s tag now reads Cube1, if not, select the drop down again and select the newly added tag.
Get to Coding
Inside our MovingObstacles script, I’ve added two variables and a private method called updateCubePosition which is called from the update method. So every frame we check if our game is currently running, if it is, then we call updateCubePosition. This method accepts a min and max, which is used to randomly select an X value so the cube will be changing its position on the X axis each time. Once the cube has passed the player, we increase the speed of the cube and then reset it’s position and then randomly pick a new X value so it’s not in the same place.
Now that we have objects moving towards our player, we need to check for when the player and the cube collide with each other. Start by creating a new script called ColliderHandler and add this method to the script. OnCollisionEnter is a method provided by Unity that will be called when ever a collision occurs. NOTE: The last line I have is SceneManager.LoadScene(“Game Over Scene”), what I’m doing is when the player hits the cube, I go to a new scene that simply says “Game Over” and then has a replay button. You can choose to do this as well or just have a console log to indicate that your code actually works.
We have our script for handling collisions, now we must assign it to our game objects.
Lets start by selecting the cube, in the Inspector we should already see a Box Collider, but we need to also add a Rigidbody script. So go to Add Component -> Physics -> Rigidbody, then uncheck the “Use Gravity” option, and check “Is Kinematic”. Then go back to Add Component -> Scripts -> Collider Handler. You should have something that looks very similar to this.
NOTE: We also need to add the Collider Handler script to the ThirdPersonController object following the same steps we just did for the cube.
Controlling the Character
If you run the game right now and use the arrow keys on your keyboard, you should be able to see your character running all over the place. This is great, but we need to limit some of these movements for our game. For one, we need to prevent the character from running towards the actual cube, we only want the character to run left and right. So in order to do this, lets edit the Third Person Character Script that is on the ThirdPersonController. To do this, select the cog icon on the far right of the script name and select Edit Script.
Once inside the script, we need to find the Start method, which should be towards the top. All we want to do is add a parameter to the constraints of the rigid body (RigidbodyConstraints.FreezePositionZ) which will freeze the player and stop them from running towards/away from the moving cube. Here is the end result,
Now if we run the game, the player can only run left and right. You can move the character away from the camera if he appears to be too close.
The character moves great when running on the plane, but if he walks too close to the edge he will fall off the plane which is a no no. So to prevent this, we will add a cube to both sides of the plane, and then remove its mesh so it appears invisible. Start by adding a new 3D cube to your scene. Then scale the Z and Y axis so the cube becomes a long and tall rectangle. Then position the cube on the left side of the plane so there are no gaps between the floor and the wall. Now with the cube still selected, go to the Inspector, and uncheck the Mesh Renderer like so. Now your cube should appear invisible but will still have the green box collider when selected. Do the exact same for the right side of the plane and your character should always stay on the plane.
Where Do The Cubes Comes From?
If you run the game now, you’ll still notice that after avoiding the cube, you will see it reappear in front of you, not very realistic you might say. To combat this, we will add a Particle System. To do so, go to the top tool bar and click GameObject -> Particle System. Select the particle system and change its X and Y position to 0, and its Z to -20, which will place it just in front of the cube. Now in the Inspector, change the Start Size to 30. This will create a wall of fog that hides the appearance/creation of the cube!
This should leave you with enough information to edit the cubes and character to your liking. The only thing I will leave you with is if you want to add more cubes, all you have to do is add the cube to your scene, assign it a tag and then in the MovingObstacles script, make another call to the method updateCubePosition with the new tag you just created. Also make sure to add the ColliderHandler script to each cube you add!