Tag Archives: platformer

Staying on the Pixel Grid

Working with 2D Toolkit in Unity is great because if you need to make a 2D game that’s pixel-pefect (such as a pixel-art platformer like Agro-Aggro), then it’s as easy as a setting the Pixels Per Meter setting on 2D Toolkit’s camera.

2D Toolkit’s camera has another setting called Zoom Factor if you want your game to have big, chunky pixels. So, for example, if you want to have your game running at 1280×960, but you want the game to seem like it has a resolution of 320×240, you’d set a Zoom Factor of 4. And lets say you have set your Pixels Per Meter to 16. That means your game’s camera is going to be viewing 20 units by 15 units of your game space.

This is all well and good, if everything in your game is only moving in one-unit increments, which is not likely to happen in a platformer where things are sliding around in float-value increments through your game space. So, I needed a way for the sprites attached to the player’s game object to stick to that magnified pixel grid. Turns out it’s actually pretty simple.

Here’s the code….

using UnityEngine;
using System.Collections;

public class PixelGrid : MonoBehaviour {
	Transform player;
	private float offsetX, offsetY;
	private float pPM; // pixels per meter
	void Start() {
		player = transform.parent;
		offsetX = transform.localPosition.x;
		offsetY = transform.localPosition.y;
		pPM = tk2dCamera.Instance.CameraSettings.orthographicPixelsPerMeter;
	void Update () {
		Vector3 playerPos = player.position;
		playerPos.x = Mathf.RoundToInt(playerPos.x * pPM) / pPM + offsetX;
		playerPos.y = Mathf.RoundToInt(playerPos.y * pPM) / pPM + offsetY;
		transform.position = playerPos;

Keep in mind that the sprite is parented to the actual player game object. So the character controller is what’s moving in float-values through the game space, but the game object that holds the sprite is a child of that character controller. The above code takes that child sprite and ensures that the sprite is only moving in game space increments equal to whole (zoomed) pixels.

Rowan’s Game

My son Rowan wants to make a game. After making Agro-Aggro, I mentioned to Rowan that if he wanted to do the art for his own game, I could turn it into something playable. He lit up when I mentioned this. He’s now full-swing into designing his game.

He’s decided that the basic game mechanic (besides being a platformer) is that you have to fight the enemies with your gun that has a sort of a rock-paper-scissors relationship with the enemies. For instance, there are fire enemies, and you kill them by firing water onto them. The water puts out the fire, but turns to steam, which floats up, condenses, and then drips back down as water droplets for you to pick up (more water ammo). Then there are water enemies, so you fight them by shooting seeds into them. The seeds grow inside the water and turn into cool plants that immediately bloom and drop new seeds for you to pick up (more seed ammo). Finally there are plant enemies. You kill them with fire, and their ashes fall to the ground, out of which little fiery phoenixes are born which you pick up (more fire ammo).

He’s already started on the concept art and will do the actual game art using my Bamboo tablet on the computer. He’s super excited about making the game. I wonder what he’ll call it?