Final Lecture 3 Part1
Final Lecture 3 Part1
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:
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.
● 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
14