2D Unity Part 1

Create sample sprites and add to Unity

  1. Clone the base Unity project: https://github.com/pvcraven/2022_Class_2D_Project

  2. Create sprites in Aseprite.

    • Use NES palette

    • Create a 16x16 character.

    ../../_images/paul_character.png
    • Create a 16x32 tree. (Or some other size, keeping in mind 16x16 is the character size.)

    ../../_images/paul_tree.png
    • Save to Assets/Sprites/Trees or Assets/Sprites/Characters folder.

    • Call your character tree_name or character_name. Obviously, use your first and/or last name, not “name”.

    • Export your sprite as a .png in that same folder.

  3. Open in Unity, confirm the assets are there.

  4. Do a git add, commit, push and pull to sync with the whole class.

Warning

Be careful of .meta files

Unity adds a .meta file that tags a GUID for each file. If you create or move a file into a Unity project, let unity create a .meta for it before check in! This includes the exported .png. Failure to do this will cause a lot of merge headaches.

Change sprite settings

  1. Create your own scene. Call it scene_name.

  2. Drag character onto the screen.

  3. Way too small. Unity defaults to 100 pixels to one ‘unit’ which is 1 meter. Change from 100 to 16.

  4. Great. Now the character is blurry. Change the filtering to ‘point’.

  5. Character might be blotchy. Turn off compression.

  6. Should be able to run the scene and see character properly.

  7. Repeat these steps for your sprites. Don’t do this for other people’s sprites.

  8. Sync with GitHub.

Make sprites solid

  1. Add a rigid body 2d. Run the game. Character should now fall.

  2. Zero out the gravity.

  3. Add to your character, the MyCharacterController script that is already in the project under the scripts folder. Examine the script and see how it works.

  4. Should be able to move character with WSAD. Can adjust speed as needed.

  5. Add your tree.

  6. Try running. No collision.

  7. Add colliders to the character and tree.

    • There are circle colliders, capsule colliders, box colliders. Pick the best one.

    • You might not want to make a collider around everything for a more 3D look.

    ../../_images/tree_collider.png
  8. Try running. Character spins!

  9. Freeze rotation.

    ../../_images/freeze_rotation.png
  10. Character may or may not appear behind/ahead of the tree properly. You can use sort mode in project settings to fix:

    ../../_images/sort_order.png

Add in score

Add in a sprite to increase your score.

  • You’ll need a collider. Make the collider a “trigger”.

  • You’ll need to add in the ScoreScript. Examine this script and the character controller together to see how they work.

  • Set the points for the score script.

    ../../_images/score.png
  • Test.

  • You can also have items that make the score go down by putting in a negative number for points.

Add in scene change

Create a sprite that will will cause you to go to the next level.

  • You’ll need a collider. Make the collider a “trigger”.

  • You’ll need to add in the SceneChangeScript. Examine this script and the character controller together to see how they work.

  • Your scene must appear in File…Build Settings. This is where you determine the order of levels. As this is a common area, only one person can edit at a time. So let the instructor do this in class.

    ../../_images/build_settings.png

Summary

This should step you through most of what you need to complete 2D Assignment 1. Expand what you’ve learned to create an explorable level. Don’t worry about the background image yet, we’ll get to that with tiles.