title | marp | paginate |
---|---|---|
Unity Cookbook. 2D Basics |
true |
true |
- Manual: 2D or 3D
- Manual: 2D and 3D Mode settings
- Manual: 2D Game Development Quickstart Guide
- Unity is first and foremost a 3D engine (x, y, z)
- there is no dedicated 2D (x, y) mode
- There is a dedicated
Vector2
class for 2D vectors- Some vectors used in 2D, like
transform
are stillVector3
- Some vectors used in 2D, like
- When creating a new project, you can choose a 2D project template
- IT'S STILL SECRETLY 3D, the z axis is just disregarded
- It basically just adjusts the Unity UI
- some 2D GameObject templates, like sprites and tilemaps available
- You can change between 3D and 2D view modes by pressing the 2D button in the Scene view
- Sprites
- 2D Primitives
- For drawing basic shapes in 2D
- Manual: 2D Primitives
- Sprite Shape Asset
- Curve-based graphics & collision
- Manual: Sprite Shape
- Colliders
- See: Collision
- Remember to use the 2D versions of colliders:
- PolygonCollider 2D, CircleCollider 2D, ...
- RigidBody 2D
- See: Physics
- Effectors
- Create a .png image file
- Add it to your Assets folder
- Click it once to see its properties in the Inspector. Set the values:
- Set Texture type to Sprite (2D and UI)
- Pixels per unit value tells how big the sprite should be on screen
- If you're using pixelart, you need to take some more things into consideration
- See: Pixelart in Unity
- Click Apply
- Drag it from the Project window into the Sprite field in the Sprite Renderer component
- Line Renderer
- Use for drawing a line on screen
- Manual: Line Renderer
- Trail Renderer
- For drawing trails
- Manual: Trail Renderer
- Sprite Shape Renderer
- Draw images along a path, or just paths
- Sprite Shape Renderer
- Brackeys video: 2D shooting
- For slow bullets: Instantiate a bullet prefab
- For instant hit: Raycasting (see example below)
RaycastHit2D hit = Physics2D.Raycast(line.transform.position, line.transform.up, lineDistance);
For grid-based movement, see this thread: Unity answers: Moving on a grid