Unity Recipes đŽ
Common Implementations
Game Inventory
- Array or List? Fixed-size?
- Each entry is a slot?
-
Swap(index, index)
: swap two items. Don't if they have the same index? Handle merge items if they are stackable. -
GetTotalQuantity(item)
: Don't forget to check every slot. -
HasItem(item)
-
AddItem(slot)
: add or increase stack -
RemoveItem(slot, q)
: remove a quantity from the inventory. Check every slot if needed. -
RemoteAt(index)
-
GetSlotByIndex(index)
Platformers
A platformer is a game such as Super Mario Bros, in which a player is jumping/climbing between platforms, to reach a goal.
We are considering that you are in Unity 3D, without the Z-axis.
- đ: Add a RigidBody to the player
- đ: To move the player, use forces (up=jump, left/right=move)
- đ: Fix the rotation (X, Y, Z) => prevent unexpected rotation
- if you can't freeze the rotation, you can create an empty game object. Using a script, update the empty game object to match the player transform. Then, simply place the camera in the empty game object.
- đ: Fix the position (Z) => won't fall
- đ: Remove frictions => otherwise, the player can stay in the air, by clamping a block
- đ: you may nest the camera inside the player, to make the camera follow the player. You can tweak the Z-axis to move the camera near or far from the player.
- đ: to check if the player is on the ground, you can check if the player is colliding with the ground
- đ: if you are using OnColliderEnter/OnTriggerEnter, beware of a bug when you are both exiting and entering a new block
Breakout
This game is called
casse-briques
in French. "In Breakout, a layer of bricks lines the top third of the screen, and the goal is to destroy them all by repeatedly bouncing a ball off a paddle into them." (Wikipedia)
We are considering that you are in Unity 3D, without the Z-axis.
- đ: Create 3 walls (left, up, right) with a collider
- đ: Create a paddle (ex: flat cube) with a collider and a rigid body
- đ: freeze Z-axis
- đ: freeze rotation (X, Y, Z)
- đ: move the platform by checking the position X of the mouse
- đ: you may use Camera.ScreenToWorldPoint
- đ: Create a ball (ex: sphere) with a collider and a rigid body
- đ: make the ball interpolate (smooth collision)
- đ: freeze Z-axis
- đ: make the ball bounce when colliding
- đ: using Vector3.Reflect
- đ: destroy the ball when lost
- đ: check if the renderer is visible
- đ: Set the camera mode to orthographic
- đ: Create a brick (ex: flat cube) with a collider
- đ: the brick should be destroyed after X hits
- đ: change color when hit, revert back after X seconds
- đ: add indestructible bricks
- đ: Create a level using bricks
- đ: Game Manager
- đ: handle points
- đ: handle lives
- đ: handle levels
- đ: create the ball and the paddle
- đ: add menus
- đ: state machine (Play, GameOver, LevelCompleted, etc.)
- đ: update the UI in the setter of points/lives/levels