Pages

Tuesday, August 12, 2014

Turn based RPG with Unity 2D - Part 3: Lighting The Map and Pixel Perfect Camera


First, let's do some changes to our script and make the sprites look crisp whatever the resolution (provided that the aspect ratio stays the same in fullscreen mode).

Open the CamController script and add this line to the Start method:

Camera.main.orthographicSize = Screen.height / 2 * 1 / 32;

Basically we just apply the math from the last post to the camera's size variable.


You'll notice the difference in higher resolutions in fullscreen mode.

Also, if you didn't do it before, select the player sprite from your assets and in the inspector, change the Filter Mode to Point and hit Apply. This will make the sprite look like it's meant to look:




Lighting the map


To add some dynamic lighting to our map, create a new material in the assets. Like so:



In the inspector, change the shader to Sprites/Diffuse:


Now select the map from the hierarchy (the object with grass grid) and in the inspector, change its material to this new material:


You can add the material to anything you want to be affected by light but let's leave the player sprite alone for now because he's always illuminated. The player sprite might disappear after you add the material, so bring him up by adjusting the Order in Layer (or adding another sorting layer for the player altogether):



Next, add a new point light from the GameObject / Create Other menu:



You must adjust the light so it shows on the scene. In the hierarchy window, drop the light object to the player object (this will make it the player object's child and it will follow the player). Also use the arrows on the light object to move it to the center of the player object:



Change the light object's Z-value to something negative to make it show on the map. The higher the number (to negative), the farther the light goes from the player. Set it to -1.5 for now. You can experiment with it for better results.

You can also move the light in the Z-axis in 3D mode. Just click the little 2D button on top of your scene window to toggle the mode.


To make it really dark, like night time, change the ambient lighting from Edit / Render Settings:



The result:



Thanks for reading! More tutorials coming up.

---

Part 1: Grid movement
Part 2: Camera control
Part 3: Lighting
Part 4: NPC AI

Code highlighted with: hilite.me

1 comment: