Showing posts with label 2D. Show all posts
Showing posts with label 2D. Show all posts

Tuesday, September 16, 2014

How to make a sprite for 2D Games

Sprite not only supports the object to move but also some 2D animations like explosion, projectile fly, etc.

Prefabs can be reused in the game with just drugging it into "Hierarchy". Here I will show how to make a sprite prefab for Unity.

1. Create a Sprite:
            Choose "GameObject" -> "Create Other" -> "Sprite" as the image bellow shows:

2. Rename the object.
3. Choose the image imported in the "assets" then change the "texture type" to "Sprite(2D\uGUI)":

4. drug the image onto the sprite object and change the z value to "-1", then it will show on the screen.

Some times we also need to use sprite sheet to implement animation like explosion. Here are the steps to make that.


1. Choose the explosion image, change its texture type into Sprite and also modify its  Sprite Mode to "Multiple":

2. Click "Sprite Editor" and slice the sprite sheet:

3. Create a Sprite and name it as "explosion". Add the material to "explosion":

4. Click "Window"-> "Animation"->"Add Curve"->"Sprite Renderer"->"Sprite":



5. Drag the other slices into the Animation Editor:

6. Modify the animation attributes as the images shows:

7. Play and we will see the animation. However, when the animation finishes playing, the object stays at the last image. We want to delete the object, so we need to modify the animation.
  • Add a script to the object:
  • Add an event on the animation and choose the function:

8. Run the game and after animation completes, the object is destroyed.

Set MainCamera to adjust 2D game

Unity is also a good tool to develop 2D games, however it is not convenient to design the game on an initial camera shape which is shown bellow:


So, firstly, let's change the shape of MainCamera:
  Change the value of "Projection" from "Perspective" to "Orthographic" shown as bellow:


Then we add directional light and a background of the scene:


  • Since the value of z axis controls the layer of the game, so the items of the game must have the same z value! 
  • The item with smaller value of z axis is put in front of the bigger value one.