0% found this document useful (0 votes)
90 views

Developing Maze Game Components in Unity

- The document discusses developing components for a maze game in Unity, including moving a ball through the maze. - In Part 1, the tutorial covers creating a ball object, plane, and materials. The ball is made a rigidbody so it can be moved. - Part 2 adds C# script to move the ball using keyboard inputs. Cubes are added as obstacles for the ball to pass through. - The finale tutorial combines everything to shape it into a full maze game, with walls and textures added to create the maze structure for the ball to navigate from start to end.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Developing Maze Game Components in Unity

- The document discusses developing components for a maze game in Unity, including moving a ball through the maze. - In Part 1, the tutorial covers creating a ball object, plane, and materials. The ball is made a rigidbody so it can be moved. - Part 2 adds C# script to move the ball using keyboard inputs. Cubes are added as obstacles for the ball to pass through. - The finale tutorial combines everything to shape it into a full maze game, with walls and textures added to create the maze structure for the ball to navigate from start to end.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Developing Maze Game

Components in Unity - Part 1


In this tutorial, we will discuss step by step, how to proceed with the development of some
components of the game. These components of the game and the logic associated with each one
of them is important to understand. We will start with moving a simple game component which
will be our player or the lead object for this game. Yes, I'm talking about the ball, the ball which
has to cross the maze. Come on, buckle up!

Start developing Game Components in Unity


Follow the steps mentioned below to develop a simple game component. Our game component
will be a ball, and we will learn about its various properties where we will add color, adjust its
size and then later move the ball using the cursor keys of our keyboard.
Some quick points to remember,

1. Open Unity 3D (4.5 or above version).


2. Check all the menu option once, just to become friendly with them.
3. You can save the scene of your project by going to File → Save Scene and give it a name
and appropriate location.

Now let's start working on Game components

1. Go to File → New Project.


2. Give your project a name, we think Ball-Motion is a nice name and set the path where
your project will reside.
3. Go to GameObject Menu → 3D object → Plane. You can also get that from
the hierarchy view using Create menu.
4. From the hierarchy window pane, you can give your Plane a new name -
example ground.

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.

Game Component's Movement and


Play in Unity - Part 2
In the last tutorial we have already seen how to create the game components and
objects and adjust their position, color and size, which was like laying foundation for our
maze game. Also we have worked on adding a new material which helps in adding
color to the objects.

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.

Stepwise Development of Maze Ball Game


So the idea is to create a maze with a ball which has to cross the maze. Sounds
simple? Well then let's continue.

Making the Ball move

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

components in the inspector pane.

Page 5 of 24
4. Under the Project Pane, create a new folder, Create → Folder and name it Scripts.

Here we will keep our C# code files.

5. Now, go to Assets menu → Create → C# Script or from Create menu

under Project view pane.

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

double click on the script asset.

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

it. Write the following C# code:

using UnityEngine;

using System.Collections;

public class ball_move : MonoBehaviour {

public float speed;

private Rigidbody rigb;

void Start()

rigb = GetComponent<Rigidbody>();

void FixedUpdate()

float moveHorz = Input.GetAxis("Horizontal");

float moveVert = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHoriz, 0.0f, moveVert);

rigb.AddForce(movement * speed);

9. Here Vector3 is used to add a force to your rigidbody component (sphere) from all (x,

y, z) axis directions. Compile the program, to check for errors.

Page 7 of 24
10. Now, go back to Unity and run the program. To play your game, you have to click

the Run button.

11. Now use keyboard's cursor keys to move the ball.

Adding Cubes as obstacles


Now it's time to add obstacles to our game, using the 3D cubes.

1. Go to GameObject menu → 3D object → Cube

Page 8 of 24
2. We will place the cubes around the sphere and the sphere will have to pass through all

of them one by one. Like this:-

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

shortcut key Ctrl + D.

Page 9 of 24
4. Place these cubes as you want or as shown in the above figure. You can add color also

using the material.

5. For tilting the cubes, you can select all the cubes and change their rotation

transformation.

Maze Game Development with Unity


- Finale
As you have already seen and understood the setup for the ball movement and setting
up of obstacles in our previous two tutorials, it's time to combine everything together
and shape it into a Maze game.
As you all know, a maze is a structure surrounded by walls with numerous paths and
passages, making it difficult for anyone to pass from one end of the maze to the other.
Just like the actual maze, in this tutorial, we will develop a game which will have big
walls as border and small internal walls to create confusion for the player.
In our game we will use the ball movement concept which we created in Part
1 and Part 2 of this tutorial and in addition, the ball will have to pass through the walls,
exploring the correct path from starting location to the end point using the keyboard
cursor to move the ball.

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.

Animation using Unity Engine


In the last few tutorials we have learnt how to create game objects and how to arrange
them to create simple games.
In this tutorial we will be dealing with the animation/motion object. We will learn
how parent object and child objects are interrelated. Alongside the game itself, Unity is
also used to develop the animation (for storyline and in game action) for the game.

Solar System using Unity Engine


Our solar system comprises of 8 main planets and 1 dwarf planet (Pluto). Also we have
natural satellites revolving around most of these planets. Planet Earth is the 3rd planet
and it revolves around the Sun. The Moon, which is a natural satellite of planet Earth,
revolves around it.
In this chapter, we will make a similar animated model of the Sun, the Earth and earth's
Moon, along with some outer space scenery using an image along with a background
music.
Follow this step-by-step procedure to start developing this 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

8. You can adjust the intensity of the Spotlight by modifying


the intensity and bounce intensity attributes.
9. Adding the required textures to our spheres, will make the whole animation look
more realistic, so if you haven't already added it, do it now.
10. Now, it's time to give the background a dark galaxy look. Download a texture or a
high quality picture, like one below.

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.

olar System Animation Script


After bringing the spheres which will act like planets and giving these planets light through sun,
and then setting up the universe-skybox for the entire scene, now i's time to rotate and revolve
the planets and their natural satellite. For rotation and revolution you have to write a C# code,
and apply it onto your game objects. So you have to apply some required steps to implement the
rotation and revolution. Let's start working on the motion part.

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

click Create > C# Script

2. Now you have the following code:

3.

4. using UnityEngine;

5. using System.Collections;

6. public class RotateAround : MonoBehaviour {

7. public Transform target; // the object to rotate around

8. public int speed; // the speed of rotation

9. void Start()

10. {

11. if (target == null)

12. {

13. target = this.gameObject.transform;

14. Debug.Log ("RotateAround target not specified. Defaulting to


parent GameObject");

15. }

16. }

17. // Update is called once per frame

18. void Update ()

19. {

20. // RotateAround takes three arguments, first is the Vector to


rotate around

21. // second is a vector that axis to rotate around

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.

27. Save the C# code and compile it.

28. Now select the Sun (biggest 3D sphere) and go to Inspector Pane > Drag and Drop your C# script

onto the Sun-sphere.

29. In the inspect Pane, under Rotate-Around Script, change the value of Speed attribute from 0 to

4, and the target value will be the 'Sun' object

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

32. Set the 'Speed' attribute value to 4 or 5

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

Speed attribute to 10 in case of moon's rotate-around script

And you are done. Your animation will start working if you have properly followed the steps
mentioned above

Page 24 of 24

You might also like