Developing Maze Game Components in Unity
Developing Maze Game Components in Unity
Page 1 of 24
5. You can reset and transform components from the Gear menu under Inspector Pane and
click Reset option. This will place the game object at the position 0, 0, 0 which is also
the origin of your game world.
6. Now if you want to change the scale of your plane, you can do it in multiple ways. You
can directly select the scale tool from the toolbox, then grab the axis handle and use your
mouse to change(move) it. You can also select any one of the three colored (red/ green/
blue) axis for changing the size of the object. You can do this for any object.
7. Now in our ball game, our ball (game object) will act like the player. To bring this ball
into picture, Go to GameObject → 3D object → Sphere.
8. You can resize it by selecting scale tool and then dragging its three dimensional axis or
from the inspector pane, just change the scale.
9. Now drag the ball upwards so that it just touches your pre-existing plane/ground which
you have already created. Doing this will move your ball over the plane after
the movement related C# coding is done.
Page 2 of 24
10. By default there will be light and camera positioned in your Unity Scene.
11. To add color or texture to your game objects (here plane and sphere), you have to
use materials. So to keep the materials create a new folder in your project. Go
to Project pane and select Create menu and then Folder. Rename it as Materials. Again
go to, Create → Material → and give it a name....
Page 3 of 24
12. Just like in the above figure, you can create materials, the select that
material, click on Albedo (which holds the whole range of colors) and choose any color
and give each of your game objects a different color. To associate that color to your
game object, you have to drag that material onto the game object.
13. You can change the color of the material whenever required.
In the next tutorial we will learn how to move the ball and pass it through some randomly
placed cubes, which will later form the maze. All these things will be designed and coded, and
you will have lots of fun during this basic game component development tutorial.
Page 4 of 24
In this tutorial we will learn how to move the ball and add cubes as obstacles and let
the ball pass through them.
1. To move the ball, you have to use the concepts of physics. So, the game object (ball)
must have the RigidBody component attached to it.
2. So to apply this, select the game object (sphere) and then go to Component menu →
Physics → RigidBody. This will attache the RigidBody property to the selected game
component.
3. Using the context sensitive gear menu, you can shift up or down the order of the
Page 5 of 24
4. Under the Project Pane, create a new folder, Create → Folder and name it Scripts.
6. Otherwise, you can also directly select the player (sphere) Game object, and then click
the Add Component button under the inspector Pane. Add Component → New Script
→ name your C# script... (for example: ball_move)
7. Upon doing this, Unity will create and attach the script to our selected game object. Now
Page 6 of 24
Note: Visual Studio must be installed in your computer in order to write C# code.
8. The script will get loaded in Visual Studio, when we open the script by double clicking on
using UnityEngine;
using System.Collections;
void Start()
rigb = GetComponent<Rigidbody>();
void FixedUpdate()
rigb.AddForce(movement * speed);
9. Here Vector3 is used to add a force to your rigidbody component (sphere) from all (x,
Page 7 of 24
10. Now, go back to Unity and run the program. To play your game, you have to click
Page 8 of 24
2. We will place the cubes around the sphere and the sphere will have to pass through all
3. To add more than one cube to the scene, you don't have to repeat the step 1. There's a
shortcut for this. You can either go to Edit menu → Duplicate option, after selecting the
game object you want a copy of (here, cube) or you can select the cube and press the
Page 9 of 24
4. Place these cubes as you want or as shown in the above figure. You can add color also
5. For tilting the cubes, you can select all the cubes and change their rotation
transformation.
Page 10 of 24
Start Developing the Maze Game
Follow the below mentioned steps to create your simple maze game. So let's begin:
1. First of all, you have to create the 3D plane on which the entire maze will reside.
2. Bring in a ball of any size and make it a rigidbody, and add C# code for
movement, just like we did in the previous tutorial
3. Add materials and color to the game objects.
Page 11 of 24
4. To add the walls (3D cubes), go to GameObject menu → 3D object → Cube.
5. Now adjust the size of the cube/wall according to your ball size (probably little bit
larger than the size of the ball), increase the scale of Y coordinate or
keeping Y coordinate intact, increase the X coordinate.
6. In the last tutorial we learnt how to create copies of any game object using
the duplicate option; which we will use here. Select the game object and go
to Edit menu → Duplicate
Page 12 of 24
7. Design the maze structure by arranging the cubes on the 3D Plane, making the
whole setup just like a maze.
Page 13 of 24
8. We can use images to make the maze walls and the 3D plane look more
realistic.
9. You can download the grass or wall texture (which is basically an image) and
add it to the Assets pane by directly dragging the image from your desktop and
dropping it onto any folder in the Asset pane.
10. Now, select that texture asset and drag it onto the walls to give your wall, a wall
type look and the plane, a grassy feel.
11. Run the game using the play button.
Page 14 of 24
As we have already covered how to add audio to Unity, you can add any music or
sound effects to this game. In the next tutorial, we will develop another new mini game
project involving solar system animation.
1. Open Unity 3D
2. Start new Project and give your project a name. For example: mini_solar_sys
3. Now, add three 3D spheres for Sun, Earth and Moon. (If you want to add other
planets like mercury, venus, mars etc, you should...)
4. To add spheres, go to GameObject menu → 3D object → Sphere and give
them different(any) X, Y and Z scale value to arrange them as per requirement.
Keep their sizes different, with the Sun being the largest, then the sphere for
Earth and then the Moon,
Page 15 of 24
5. You can browse for textures on the internet for Sun, Moon and Earth and apply
them to our game objects. You can also find these assets in the Uniy 3D asset
store like this one.
6. In this animation scene/model, we will have to add two types of lights, first
the Spotlight for illuminating other objects from a single point, which in this case
is the Sun. The other one will be the Area Light which will illuminate the whole
scene, acting as the glow of the sun
Page 16 of 24
7. To bring these two lights into action, go to GameObject menu → Light →
Spotlight
Page 17 of 24
11. To cover the complete scene background, we will have to use skybox. The
skybox fills the entire unity background with a texture in such a way that the
entire background looks alike and similar.
12. To achieve this in our animation scene, we will have to use material. Go
to Project Window Pane, Assets menu → Material, name it background.
13. Now select the material, and go to Inspector Pane, and choose, Shader drop
down → Skybox → 6 Sided
Page 18 of 24
14. Click on Select → Select Texture, a window pops up, select the universe
background texture from the assets and close the window.
Page 19 of 24
15. You will have to repeat the above step of adding texture, six times, to add this
texture to all the sides of the 6 Sided Skybox → Front, Back, Left, Right, Up and
Down.
16. Now simply drag and drop the newly created material on to your Unity
Background, you will see something like this,
Page 20 of 24
In the next lesson we will continue to discuss about this and add scripts and codes
which will allow us to move the planets and perform the concepts of rotation and
revolution in the form of animation.
Page 21 of 24
Stepwise Representation of Rotating and Revolving
the Spheres
Follow the below mentioned steps to apply motion in your game.
1. Go to Asset Menu under Project Window Pane and add a separate Folder. Inside that folder
3.
4. using UnityEngine;
5. using System.Collections;
9. void Start()
10. {
12. {
15. }
16. }
19. {
22. // third is the degrees to rotate, in this case the speed per
second
Page 22 of 24
23. transform.RotateAround(target.transform.position,target.transf
orm.up,speed * Time.deltaTime);
24. }
25. }
26.
28. Now select the Sun (biggest 3D sphere) and go to Inspector Pane > Drag and Drop your C# script
29. In the inspect Pane, under Rotate-Around Script, change the value of Speed attribute from 0 to
30. Now in the Hierarchy window, drag the Moon-sphere game object on to the Earth-object, you
will see that the Earth got a sub-tree and inside it the moon-sphere object will reside. This
eventually creates a relation between the earth and the sun. So when the earth revolves around
the sun, the moon also revolves around the sun, under earth as a sub part of earth
31. Now again use the same C# code and drag and drop it onto the Earth-sphere game object. This
time use the Rotate-Around script will appear on the Inspector pane twice. Once for the earth
itself to rotate around its own axis (in this case you have to drag and drop the C# script on to
earth game object and then the Earth object from Hierarchy Window pane
to 'Target' attribute under Inspector-Pane in Rotate-Around script section) and the other on
the Earth game object again drop the C# rotateAround script and then drag and drop the Sun
object from Hierarchy Window Pane to 'Target' attribute of Rotate-Around script under
Inspector pane
Page 23 of 24
33. Again, for the moon game object, you have to drag and drop the C# script twice, one for the
moon itself and other for the earth upon which the moon will revolve. Like the previous step,
drag and drop the C# code on to the moon once and from the Hierarchy Window Pane, drag and
drop the moon game object to the Rotate-Around script's 'Target' attribute under Inspector
Pane. Again, drag and drop the C# script on to the moon game object and then go to Inspector
Pane. From the Hierarchy Window Pane, drag and drop the Earth game object and place it on to
another 'Target' attribute of Rotate-Around script under Inspector Pane. Also set the target
And you are done. Your animation will start working if you have properly followed the steps
mentioned above
Page 24 of 24