2D Unity Lab 5 (Camera)
2D Unity Lab 5 (Camera)
__________________________________________
5
Exercise #18 - Camera Follow
• To make the camera follow your player throughout the level,
you must create a script and attach it to your Main Camera
• The Lerp function stands for Linear Interpolation. It will help us calculate how fast the
camera will move across time, and how it will follow the player game object
• The camera’s new position is determined by taking its previous position, target’s
• position, and speed multiplied by time that passed since the last frame
Note on Lerp Function
• One way to explain Linear Interpolation is that it is a
mathematical formula designed to take 3 values: the
FROM/BEGINNING point, the TO/ENDING point, and the
smoothness by which you arrive to the ending point
• If your camera moves smoothly, but your player game object doesn’t, go to its
Rigidbody2D component and turn on Interpolation.
• (Your camera movement may be a bit laggy anyway if the computer’s graphics card is
limited)
Camera Zoom
• To make your Main Camera zoom in and out, it can be done via Button
press, or Mouse Scroll
• To create Zoom via button press, create a new script and name it Cam
Zoom
Exercise #19 – Camera Zoom (Button Press)
Exercise #19 – Camera Zoom (Button Press)
(Cont.)
• Create a FixedUpdate() function and determine that if the player presses Z, the
camera’s size changes from the original orthographic size, to the smaller value (also
set by YOU)
• Use the Lerp function again to smooth the effect from value A to value B
• Else if the player removes their finger, the size returns to normal, also using
• Lerp to smooth out the movement from small to big
Exercise #19 – Camera Zoom (Button Press)
(Cont.)
• Make sure to update the values of your public variables in
Unity and test your game
Exercise #19 – Camera Zoom (Mouse Scroll)
• Now create another Script and call it something relevant, like
Mouse Zoom
Exercise #19 – Camera Zoom (Mouse Scroll)
(Cont.)
• Create the following variables and assign them
Exercise #19 – Camera Zoom (Mouse Scroll)
(Cont.)
• Now in the Update()function, use Input.GetAxis(“Mouse ScrollWheel”)
to gather float value input and store it in the ScrollData variable
• This effect makes your game’s background feel like a car ride:
Elements in the far horizon seem to move very slowly, while closer
elements such as trees move by much faster
• Parallax effect can be written in many different ways. The
following exercise #19 is one variation. Create a new script, name
it BackgroundParallax and attach it to the Main Camera
• Note: make sure you don’t leave spaces in a script file’s name
Extra Exercise – Parallax Effect
• Design your game’s background however you want, and make sure all the elements are
on the Background Layer
• Create a GameObject and call it Background. This will be the parent that contains all
the background elements
• Inside Background, make more divisions. For example, you can have brown
mountains at the very back, green mountains in the middle,
• and trees at the front
Extra Exercise – Parallax Effect (Cont.)
• After you’ve designed your background, go to your Parallax
script and open in Visual Studio. Declare the following variables
Extra Exercise – Parallax Effect (Cont.)
• In the Start() function, add:
• C reate a var and name it backgroundTargetPosition. This variable contains the location
the background is supposed to be at the next frame (along x-axis).
• Assign it the value of the current x-position of the background layer you are currently
at, and add the parallax factor (the camera position at that particular moment).
Multiply the current iteration (background layer) by the Parallax Scale Factor
• Inside the loop, we need a statement that makes every layer of background slower
than the layer before it.
Extra Exercise – Parallax Effect (Cont.)
• Use the Lerp function to determine the FROM value as the current background
layer’s position, The TO value which is where the background is supposed to be
(note that it takes 3 values because it’s a vector3 type), and the smoothing
factor multiplied by the time that passes between each frame and the next
when the game runs
Extra Exercise – Parallax Effect (Cont.)
• Go back into Unity and see the public variables appear as the script’s properties
• Drag the Background super parent from the Hierarchy and drop it onto the Background
property in the Inspector as seen below
• Drag each of the 3 background children and drop them into the text boxes as seen below
• Experiment with the values of the Parallax Scale,
Reduction Factor, and Smoothing
Extra Exercise – Parallax Effect (Cont.)
• After the loop ends, do not forget to update the camera’s position by adding
• once more the statement:
• So when the Update function is called against for the next frame, the camera is where
it should be
• Run your scene
References
• Unity Parallax Tutorial - Infinite Scrolling Background. URL Retrieved from:
https://www.youtube.com/watch?v=zit45k6CUMk
• Shooting Projectiles & Camera Control - Unity 2D Platformer Tutorial – Part 8.
URL retrieved from: https://www.youtube.com/watch?v=8aVZuL9ocrk
• Creating 2D Games in Unity 4.5 #15 - Camera Controller. URL retrieved from:
https://www.youtube.com/watch?v=u67fbxe8xxY
• 2D RPG Smooth Camera - Unity3D. URL retrieved from:
https://www.youtube.com/watch?v=KMhPYf9zzlA
• How to make a 2D Platformer - Parallax Scrolling - Unity Tutorial. URL
Retrieved from: https://www.youtube.com/watch?v=5E5_Fquw7BM
• Unity 5 2D Platformer Tutorial - Part 8 - Camera Bounds. URL Retrieved from:
https://www.youtube.com/watch?v=I6xmOMsRWeo