0% found this document useful (0 votes)
7 views25 pages

Unity 5: Camera Movement & UI Setup

This document discusses several topics in Unity including moving the camera with objects, using parent and child transforms, and adding a user interface with text to display score and a button to change scenes. It provides code examples for a script to keep the camera a fixed distance from the ball as it moves, and scripts to display the score in a text object and load a new scene when the start button is pressed.

Uploaded by

Eyad M.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views25 pages

Unity 5: Camera Movement & UI Setup

This document discusses several topics in Unity including moving the camera with objects, using parent and child transforms, and adding a user interface with text to display score and a button to change scenes. It provides code examples for a script to keep the camera a fixed distance from the ball as it moves, and scripts to display the score in a text object and load a new scene when the start button is pressed.

Uploaded by

Eyad M.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 >>>> [Link] .
• 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

Common questions

Powered by AI

The transform component is fundamental in Unity for establishing the spatial relationship between parent and child objects. When an object becomes a child of a parent, its local transform coordinates are set relative to the parent's transform. This means movements, rotations, and scaling applied to the parent will automatically affect the child as well, preserving relative layout and animations. This is crucial in gameplay for maintaining consistent animations and dynamic object hierarchies, allowing developers to create complex interactive systems with coordinated transformations .

To dynamically switch scenes using a button in Unity, first create a new scene and add a button object to it. Use the button's text label to designate its function (e.g., "Start"). Write a new script to handle the scene transition logic and attach it to an empty game object. Assign this script to the button's onClick event and select the appropriate function to call. Don't forget to list both the current and target scenes in Unity's Build Settings to ensure they are included when the game is compiled .

In Unity, the parent-child relationship establishes a hierarchical structure where one game object (parent) contains another (child). The child's transform properties such as position, rotation, and scale become relative to the parent's properties. Therefore, when you change the parent's position, rotation, or scale, the child transforms accordingly. This enables complex object grouping and coordinated transformations common in game design .

Setting up a UI system in Unity can present challenges such as ensuring compatibility across different screen resolutions, managing complex hierarchies of UI elements, and maintaining performance. These challenges can be mitigated by using Unity's UI tools effectively, such as Canvas Scalers for resolution independence, organizing UI elements in a logical hierarchy to simplify management, and reducing unnecessary UI updates or complexities to optimize performance. Additionally, testing UI on different devices or screen sizes can help anticipate and solve potential issues early in the development process .

Scripts play a crucial role in Unity for dynamically modifying the user interface during gameplay, enabling real-time updates and interactions based on game events. Scripts can change UI elements' text, color, visibility, and interactions, allowing developers to create responsive and interactive experiences that respond to player actions or game states. Additionally, scripts can efficiently manage UI animations and transitions that enhance player engagement and improve the overall user experience, creating a more immersive game environment .

Using a public GameObject variable in Unity scripts makes it easy to drag and drop references to other GameObjects via the Unity Editor, simplifying setup and reducing potential for code errors. The main benefit is ease of use and transparency in the editor. However, the drawbacks include reduced encapsulation, increased potential for unintended changes since designers or users can modify public variables, which might lead to bugs if not properly managed .

To maintain a fixed distance between a moving ball and the camera in Unity, you need to create a new script and attach it to the camera. First, calculate the initial distance between the ball and the camera and store it in a variable. Whenever the ball moves, update the camera's position by using this stored distance to adjust its position accordingly. You can save the distance by computing the difference between the camera's position and the ball's position. To do this in the script, identify the ball object using a public or private GameObject variable, which can be manually assigned or dynamically set in the script .

In Unity, encapsulation in script design is achieved by restricting access to variables and methods, using private or protected access modifiers, and providing controlled access through public methods. This is particularly relevant when scripts need to access other GameObjects. By using encapsulated properties or methods, you protect internal state, ensuring that other scripts do not directly modify critical variables. This reduces bugs and enhances maintainability. Careful encapsulation is necessary because directly using public variables can lead to unpredictable interactions and make debugging more challenging .

To display a score in a Unity game, follow these steps: First, right-click on the hierarchy and select UI, then choose 'Text' to create a text element. Use the Inspector to modify the text's position and appearance. Next, create a script and attach it to the text UI element to dynamically update the score during gameplay via code logic in the script .

Unity's scene management tools allow developers to organize different parts of a game into scenes, which can be loaded and unloaded independently. This separation of game components into scenes facilitates efficient memory usage and organized development, enabling large-scale games to run smoothly by only loading necessary parts of the game at runtime. By managing scenes effectively, developers can ensure seamless transitions between game levels or environments, and support features like load screens. These tools are especially beneficial when preparing the game for deployment, as they ensure optimized build configurations .

You might also like