Skip to content

Instantly share code, notes, and snippets.

@KraigWalker
Last active December 25, 2015 04:49
Show Gist options
  • Save KraigWalker/6919914 to your computer and use it in GitHub Desktop.
Save KraigWalker/6919914 to your computer and use it in GitHub Desktop.
Positions an isometric camera in the scene and updates it's location according to keyboard input. For the tutorial on how to create an isometric game camera visit http://bit.ly/19D0D9E
using UnityEngine;
using System.Collections;
// Camera Controller
// Revision 2
// Allows the camera to move left, right, up and down along a fixed axis.
// Attach to a camera GameObject (e.g MainCamera) for functionality.
public class CameraController : MonoBehaviour {
// How fast the camera moves
int cameraVelocity = 10;
// Use this for initialization
void Start () {
// Set the initial position of the camera.
// Right now we don't actually need to set up any other variables as
// we will start with the initial position of the camera in the scene editor
// If you want to create cameras dynamically this will be the place to
// set the initial transform.positiom.x/y/z
}
// Update is called once per frame
void Update () {
// Left
if((Input.GetKey(KeyCode.LeftArrow)))
{
transform.Translate((Vector3.left* cameraVelocity) * Time.deltaTime);
}
// Right
if((Input.GetKey(KeyCode.RightArrow)))
{
transform.Translate((Vector3.right * cameraVelocity) * Time.deltaTime);
}
// Up
if((Input.GetKey(KeyCode.UpArrow)))
{
transform.Translate((Vector3.up * cameraVelocity) * Time.deltaTime);
}
// Down
if(Input.GetKey(KeyCode.DownArrow))
{
transform.Translate((Vector3.down * cameraVelocity) * Time.deltaTime);
}
}
}
@KraigWalker
Copy link
Author

For Revision 2 I modified the script to use Translate() instead of made up position variables. This is handy as Translate() moves the camera in relation to it's own X/Y/Z axis rather than global world coordinates, so the camera will always move correctly no matter what it's rotation.

I use Vector3.direction instead of the usual syntax. These are handy helper values which save you from putting in the values. Beyond maybe using Input.GetAxis for detecting input, I don't think you can get any simpler than this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment