0% found this document useful (0 votes)
16 views15 pages

Final Lecture 3 Part1

The document discusses Unity methods for initializing and updating game objects. Awake() and Start() are initialization methods called in that order when a game starts. Update() is called whenever ready and linked to rendering, while FixedUpdate() is called at fixed intervals linked to physics calculations. Vector2 and Vector3 classes represent 2D and 3D vectors with common operations. The Transform component controls position, rotation and scale of game objects. Movement can be made frame-rate independent by multiplying speed by Time.deltaTime. GameObjects can be retrieved by name or tag.
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)
16 views15 pages

Final Lecture 3 Part1

The document discusses Unity methods for initializing and updating game objects. Awake() and Start() are initialization methods called in that order when a game starts. Update() is called whenever ready and linked to rendering, while FixedUpdate() is called at fixed intervals linked to physics calculations. Vector2 and Vector3 classes represent 2D and 3D vectors with common operations. The Transform component controls position, rotation and scale of game objects. Movement can be made frame-rate independent by multiplying speed by Time.deltaTime. GameObjects can be retrieved by name or tag.
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
You are on page 1/ 15

Unity

Lecture 3
Awake()–Start()–Update()–FixedUpdate()
● In scripts, Unity has many built-in methods inherit from the superclass “MonoBehaviour”.

● There are two special methods automatically included: Start() and Update() and two other
methods can be added to a script: Awake() and FixedUpdate().

● Awake() vs Sart()  Start and Awake are initialization methods. When the game starts, it
will execute its Awake method and then its Start method, always in that order. If we are
creating multiple GameObjects at the same time, Unity will execute each of the Awake
methods of all GameObjects and then all Start methods. In particular, Awake is used to
guarantee a value is initialized before other GameObjects use it and Start() is used for basic
initialization. Generally, both are not needed except for specific cases where order is
crucial.
2
Awake()–Start()–Update()–FixedUpdate()
● The two main game loops are Update() and FixedUpdate(). The frequency of these two
methods differ.

● FixedUpdate() is called at regular and fixed intervals. It is running very periodically (t=0.02,
t=0.04, t=0.06, etc). It is closely linked to the physics system and is called in sync with the
Physics calculations.

● Update() is called whenever it is ready. It is closely linked to the rendering system and is
called in sync with when the screen is being redrawn. Periodic calculations that do not
require fixed intervals

● Generally, FixedUpdate() is used for periodic calculations that need to be regular (for
example, object motion), and Update() is used for periodic calculations that do not require
fixed intervals. 3
Awake()–Start()–Update()–FixedUpdate()

4
Vector2 Class – Scripting
● Vector2 class represents 2D vectors. It contains functions and properties for common vector
operations (e.g., Angle, Distance, Dot, etc.)

● Main Constants:

Vector2.up: shorthand for writing Vector2(0, 1).

Vector2.down: shorthand for writing Vector2(0, -1).

Vector2.left: shorthand for writing Vector2(-1, 0).

Vector2.right: shorthand for writing Vector2(1, 0).

Vector2.zero: shorthand for writing Vector2(0, 0).

Vector2.one: shorthand for writing Vector2(1, 1). 5


Vector3 Class – Scripting
● Vector3 class represents 3D vectors.

● Main Constants: Vector3.up: shorthand for writing Vector3(0, 1, 0).

Vector3.down: shorthand for writing Vector3(0, -1, 0).

Vector3.left: shorthand for writing Vector3(-1, 0, 0).

Vector3.right: shorthand for writing Vector3(1, 0, 0).

Vector3.forward: shorthand for writing Vector3(0, 0, 1).

Vector3.back: shorthand for writing Vector3(0, 0, -1).

Vector3.zero: shorthand for writing Vector3(0, 0, 0).

Vector3.one: shorthand for writing Vector3(1, 1, 1).


6
Transform Component – Scripting

7
Transform Component – Scripting

8
Transform Component – Scripting

9
Transform Component – Scripting

10
Transform Component – Scripting

11
Frame-rate Independent Movement
● To make the movement frame-rate independent, the speed value must be multiplied by
another value called deltaTime.

● Time is a class that has properties and methods, and one of those properties is deltaTime
which represents the amount of change in time.

● Specifically, deltaTime is the amount of time between frames. The time between frames
varies at different frame rates (for example, 30 fps has a deltaTime of 1/30th of a second).

● Thus, multiplying the speed value by Time.deltaTime is important because the game
may be run on varying hardware when deployed, and it would be odd if time slowed
down on slower computers and was faster when better computers ran it.

● Using Time.deltaTime, the speed will be the same on all computers.


12
Name and Tag – Scripting
● We can retrieve and find GameObject/GameObjects by Name or Tag.

● Find(“Name”)  returns the object having the given name.

● FindWithTag(“Tag")  assumes that there is just one object with the given tag (If there
is none, then null is returned. If there is more than one, it returns one of them).

● FindGameObjectsWithTag(“Tag") returns all objects having the given tag in the form
of an array.

13
Name and Tag – Scripting
Retrieving a GameObject by Name

Retrieving a Collection of GameObjects by Tag

14

You might also like