2D Basics

Using Unity for 2D games

2D Project template

  • 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

Useful 2D GameObjects

Camera component

  • 2D camera needs to be orthographic

Collision & Physics components

Sprite Renderer component

  • 2d images in games are called sprites

  • They're drawn on screen with the Sprite Renderer component

Example: Using Sprite Assets

  1. Create a .png image file
  2. Add it to your Assets folder
  3. 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
    • Click Apply
  4. Drag it from the Project window into the Sprite field in the Sprite Renderer component

Other renderer components

Extra: Two ways for 2D shooting

  • 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);
    

Extra: Moving on a grid

For grid-based movement, see this thread: Unity answers: Moving on a grid