You are on page 1of 25

Unity 5: Roll the ball game(part3)

Unity Level

1
Content
 How to move the view of the camera with objects.
 Parent and children transform.
 UI in unity.
• Making a score (using text)
• Changing scenes in UNITY (using button).

2
Move the camera with objects
• Moving the ball changes the distance between the ball and
the camera.
• We want to make a fixed distance between camera and ball.
• Steps:
• Make a new script and add it to the camera
• Find the distance between ball and the camera and save it in a
variable
• Use this variable to change the position of the camera when the
position of the ball is changed.

3
Move the camera with objects
(cont.)

• Make this script and add


it to the camera as in the
image.

4
Move the camera with objects
(cont.)
• Make a variable and name it “Distance

5
Move the camera with objects
(cont.)
• Use this variable store the distance between camera and the
ball.
• Distance = position of camera – position of the ball.
• In the “CameraView” script:
• position of the camera >>>> transform.position .
• But how to get position of the ball in this script?
• “CameraView” script doesn’t know the sphere game object.

6
Access a game object from the
script on another game object
• To identify the sphere game object to “Cameraview” script
• There are two ways to do that:
• Way 1:
• Make a variable with data type “Gameobject”
• Make it public and drag the ball to it
• Way 2:
• Make a variable with data type “Gameobject”
• Make it private and write a code to put a ball on it.

7
Access a game object from the
script on another game object
(cont.)
• Way 1:

8
Access a game object from the
script on another game object
(cont.)
• Way 2:

9
Entire code of “CameraView”

10
Introduction to Parent and
children in unity
• In this game we make:
• Walls to hold all cubes of the wall
• Cubes to hold all golden cubes in the game
• Walls is a parent game object
• Cubes is a parent game object

11
Introduction to Parent and
children in unity (cont.)
• Any Game object inside the parents is called Child

• Child transform is relative to its parent.


• Try to change parent position, rotation or scale and notice
what happens
12
User interface (UI)

• User interface refer to the interaction between human and


computers, websites, games or applications.
• For Example: let see how to interact with Whats App
application:
• You will go to chats from here
• And show the status from here
• And find the calls from here
• Record a voice recorder from this button

13
UI in games
• User interface in Unity means that the player of your game
will interacts with the game.
• Look at this example:
• There are many user interface in the following images:

14
UI in unity
• We will add these user interfaces for this game:
• Text to view the score
• Button to start the game and change the scene

15
Text to view the score
• Steps to make a score to the game:
1- Right click on hierarchy >> UI >> text

16
Text to view the score (cont.)
2- Use inspector of the text to modify the position and
format of the text
3- Make a script and put it on the text game object

17
Text to view the score (cont.)
• 4- use the following code to make a script for the score
• In the “Score” script:

18
Text to view the score (cont.)
• 5- use the following code to make a script for the score
• In the “moveBall” script:

19
Button to start the game by
change the scene
• Create a new scene

20
Button to start the game by
change the scene (cont.)
• Rename the new scene and open it by double click

• Add a button to the scene

21
Button to start the game by
change the scene (cont.)
• Use the text of the button to write “Start”

• Add new script

22
Button to start the game by
change the scene (cont.)
• Write the code

23
Button to start the game by
change the scene (cont.)
• Add the script as follow:
• Make an empty game object
• Put the “startButton” script on it
• Put this game object to onClick and choose the onPressed
function you have created.

24
Add the scenes to the Build
• Go to file >> Build setting
• Add the two scenes to build setting windows

25

You might also like