Very Basic Top Down Camera for Unity3D

Now that it’s the weekend again I can put my day job behind me and have another go at Unity3D. I’ve been thinking all week about systems for 2D / 2.5D games using Unity, since I’m coming from a 2D background and I haven’t quite got the bottle for 3D rotations and whatnot. The first thing I’ve managed to build is a very simple top down camera. Read on to find out how.

From the basic scene, I added a cube to represent the player. I then added a new JavaScript file to the cube called ‘PlayerMove’. In the JS file I added a line before the update:

var topDownCamera : Transform;

This exposes a variable so the we can link our camera’s transform component to the script. After saving the script, select the player object, then drag the camera from the Hierarchy onto the ‘topDownCamera’ variable that is now visible in the Inspector, like so:

Now we need to set up the camera - move it to an appropriate position (I put mine at 0,5,0) and rotate it to point at the player (rotation x=90). Tweak all of the other variables to meet your needs, such as the clipping and field of view.

Finally, switch back to the JS file and add this line at the end:

topDownCamera.position = Vector3(
	transform.position.x, 
	5, 
	transform.position.z);

Put in you own movement code for the player and test it out. You should see the camera tracking the player object’s position without rotating. I know it’s simple, but it’s not a bad starting point. There are plenty of ways to expand on this. For example, you could make it so that it tracks ahead of the object, or zooms out based on the speed. Also, the ‘y’ position is set to 5 in my example, but you can set it to whatever you need, or even expose it to the editor as a variable (e.g. “var cameraHeight : float;”).

That’s all for now - back to Unity for me.