Module 4 Examples: All about the Camera and Views
Link to GitHub
·
Credits:
o
Original
Design for the Characters in Dye Hard: Nathan Evers
o
Assets
copied from
this book
Coverage
Unity specific skills you will need,
practice, and demonstrate include:
·
Working
with camera viewport
·
Changing
scenes
·
Information
persisting across scenes
·
Sprite
sheet (Atlas)
Concepts
you will explore and understand include
·
More
about the lerp function
·
Camera
views and the game world,
·
Soft
and hard movement of the camera: lerp and shake
- Basic Camera Controls:
- Run Behavior:
- N: Move camera by (x,
y) values (from the X/Y slider bars)
- M: Move camera to (x,
y) values (to where Dye is located)
- H: Zoom with respect
to the center (zoom factor slider bar)
- J: Zoom with respect
to Dye (zoom factor slider bar)
- Scene setup:
- MainCamera:
- UI-Canvas:
- X/Y/Zoom: sliders
(all connected to UserControlLogic)
- TheWorld:
has UserControlLogic
- Background: Z=1
(larger than hero and egg)
- Hero and Egg
- CameraSupport
- mTheCamea: reference to the camera
- mWorldBound: the current bounds of the world
- Behavior:
updates the world bound per frame
- functions:
- GetWorldBound(): get the current
world bounds
- CollideWorldBound(): collide a bound
(of an object) with the world bound and returns collision status
- ClampToWorldBound()
- Move/Zoom
- PushCameraByPos
- UserControlLogic: central object that receives
and parses user input
- Connection to UI
sliders and other game objects
- Transmits user input
to game objects to cause changes
- Camera folder/
- CameraSupport:
seen this before, new function: ClampToWorldBound()
- CameraSupport_Manipulate:
manipulates the connected camera
- MoveBy,
Moveto, Zoom(towards
center), ZoomTowards(point), PushCameaByPos()
- Testing:
- HeroControl (on the Hero)
- CollideWorldBound
code
- PushCamera
- EggBehavior
- ClamAtWorldBound
(always inside)
- Unity How to:
- Slicing Sprite Sheet (Atlas)
- Import Sprite Sheet (Type: Sprite (2D and UI))
- Select the Sprite
- Sprite Mode èMultiple
- Invoke the Sprite Editor, Click on Slice è Apply
- You may have to
install the Sprite Editor using PackageManager
- Search for
“2D Sprite” to install under “Unity Registry”
- Empty GameObject on TheWorld:
UserControlLogic
- Learned:
- Camera manipulation: simple changes to camera’s transform
object (position, and orthographicSize)
- Note: the partial class to
separate implementation into logical units
- Depth layering:
- Background with Z=1 (behind Hero and Egg)
- Unity: slicing of a sprite sheet
- Taking a closer look at Lerp
- Run Behavior: Z to
see Lerp
- Try type
Z-repeatedly!
- Increase to twice
current size and tries to Lerp back gradually
- If continue to
increase size before lerping to original
size, net growth in size
- This can be easily
avoided in HeroControl (how? e.g., ignore
z-key when LerpIsActive())
- Examine: TimedLerp
class
- Perform lerp for a
fixed duration
- Rate is in units of
second (easier to think about?)
- Rate and Duration are
sliders in the UI-Canvas
- Look at HeroControl
- BeginLerp() to begin
- do not need to
always SetLerpParms(), if you know the values
- LerpIsActive() to
receive proper lerp results
- Learned:
- TimedLerp:
- Pretty handy class
in general?
- This is is _NOT_ a MonoBehavior!
- Note on testing:
separate and trivial test environment, test all aspects
- More respectful camera manipulation:
- Run Behavior: same as previous examples, but notice the
damping effect!
- N: Move camera by (x,
y) values (from the X/Y slider bars)
- M: Move camera to (x,
y) values (to where Dye is located)
- H: Zoom with respect
to the center (zoom factor slider bar)
- J: Zoom with respect
to Dye (zoom factor slider bar)
- CameraSupport:
- Position Lerp
- Size Lerp
- Update()
function must check,
- If Lerps are going,
must set accordingly
- Learned:
- One simple example of
using the TimeLerp function (in lerping the camera position and size)
- Shaking … for the camera
and more
- Run Behavior: same as previous examples, added key control
- X: To shake the camera (boss is coming!)
- Utility/ShakePosition
(in Utility)
- Sampling on a DampedHarmonic function
- Number of cycles
- Length of shake
duration
- CameraSupport_Manipulate:
- SetShakeParameters
- UpdateShake()
- When done, will
return the original position
- Learned:
- Describe behavior and
search functions learn, appropriately sample functions
- More advanced camera behaviors:
- Parallax, following:
look for existing solutions (try to avoid implementing solutions that
are already out there).
- Viewport and Hero Cam:
- Run Behavior: same as previous examples
- with a small cam
focusing on the hero
- Background: located at z=30
- Main Camera
- Located Z=-10
- Far = 50
- Background is 40
units away, can see the background
- Small cam (HeroCam): Create a new
Camera
- Projection:
Orthographic
- Depth: > 0 (Main
Camera is -1, the bigger the number, the more front)
- Background color:
- Camera Located Z=-10
- Camera Far=30
- Background is 40
units away, cannot see background, thus, seeing constant color
- Check out the
Viewport setting
- CameraSupport:
- SetViewportMinPos(x,
y)
- SetViewportSize(w, h)
- UserControlLogic: testing
- Sliders connected to
the HeroCam
- Change the small view
viewport settings
- HeroControl:
- Changing the CamPosition of small view
- Questions:
- What does it mean to zoom
in the small view?
- What does the orthographicsSize of Small view mean?
- Learned:
- What is a viewport
and its settings
- How to work with the
Unity viewport
- New Scene and Game State that
persists over scenes
- Run Behavior: same as previous examples
- Perform some hero
lerp and camera shakes (see the text echo)
- L key to load new scene: notice lerp and shake info
persisted across scenes
- Unity details:
- Level Persistency: Singleton Pattern
- GameState class
- Print from UserControlLogic
- NewLevel
level
- Simply echo, value
persisted levels!
- Organization:
- Level-Specific
folders
- Level-specific
behavior: NewLevelGameManager
- Level-ClassExample
- HeroControl,
EggBehavior, and UserControlLogic
- Move Camera into
Utilities
- Learned:
- All game objects are
erased when a new scene is loaded
- Must take special
care (e.g., Singleton pattern) to create and maintain info across scenes
- Unity how to:
create/load scene
- Take a look at: