Isometric camera in javascript for Unity
This script was crafted relatively quickly. It may be missing some features and is “as-is”. I recommend you add mouse move to it, but to understand the basics i used keyboard keys. As of now Unity on windows seems to have some hard time with my logitec mouse, on my other pc with a Dell mouse, the mouse scrolling for zooming works fine.
The script exposes the following properties :
Target
the target is a game object that you will want to disable the renderer if you put a 3d mesh or cube for debugging. it is the controller of the camera. When you hit a key this target will move and the camera will move towards it in an interpolated fashion.
x_Angle and y_Angle
These properties are set at the start-up of the object. One interesting thing you could add here is dynamic editing of the camera in real-time. to see the change, restart the game.
smooth_moving
Enable this for lerp movement of the camera, that is “smoothed movements”
smooth_factor
Set this smooth factor is smooth_moving is set to true. Default is 5.0
speed
Movement of target for translation. This is always modified by the delta time (time since last frame see the code its quite explicit)
x_bound_min, z_bound_min, x_bound_max, z_bound_max
This actually the bounds of your map or level, the camera will not be able to go beyond that point in both x and z axis.
zoom, zoom_bound_min, zoom_bound_max
Those control how far the camera is from the ground (y = 0). The zoom property sets the starting zoom point.
Javascript Unity Code :
var target : Transform;
var x_Angle : float = 45.0;
var y_Angle : float = 45.0;
var smooth_moving : boolean = true;
var smooth_factor : float = 5.0;
var speed : float = 5.0;
//maximum bound for x level (maximum going for camera on the x plane)
var x_bound_min : int = -50;
var x_bound_max : int = 50;
//maximum bound for y level (maximum going for camera on the y plane)
var z_bound_min : int = -50;
var z_bound_max : int = 50;
//for zooming
var zoom : int = 15; //initialize the position based on the magnitude of the vector.
var zoom_bound_min : int = 15; //minimum zoom magnitude
var zoom_bound_max : int = 65; //maximum zoom magnitude
var SeeDebug : boolean = true;
@script AddComponentMenu("Camera-Control/Smooth Isometric Camera")
@script RequireComponent(Camera)
private var targetDir : Vector3; //Get this after initial placement and then calculate z
function Start ()
{
if(!target)
{
Debug.LogError("You must have a valid game object as target to isometric camera, put it at 0,0,0");
}
else
{
transform.Rotate(x_Angle, y_Angle, 0);
}
}
function LateUpdate () {
if(target)
{
targetDir = -transform.forward zoom;
var finalZoomedDestination : Vector3 = target.position + targetDir;
if(smooth_moving)
{
//smooth moving of camera.
transform.position = Vector3.Lerp(transform.position, finalZoomedDestination, Time.deltaTime smooth_factor);
}
else
{
transform.position = finalZoomedDestination;
}
}
}
function OnGUI()
{
if(SeeDebug && target)
{
GUI.Label (Rect (25, 25, 200, 30), "Cam position:" + transform.position.ToString());
GUI.Label (Rect (25, 50, 200, 30), "Target position:" + target.position.ToString());
GUI.Label (Rect (25, 75, 100, 30), Input.GetAxis("Mouse ScrollWheel").ToString());
}
}
function Update() {
if (target)
{
if(Input.GetKey(KeyCode.W) && (target.position.z < z_bound_max))
{
target.position += (Vector3.forward speed) Time.deltaTime;
}
if(Input.GetKey(KeyCode.S) && (target.position.z > z_bound_min))
{
target.position -= (Vector3.forward speed) Time.deltaTime;
}
if(Input.GetKey(KeyCode.A) && (target.position.x > x_bound_min))
{
target.position -= (Vector3.right speed) Time.deltaTime;
}
if(Input.GetKey(KeyCode.D) && (target.position.x < x_bound_max))
{
target.position += (Vector3.right speed) Time.deltaTime;
}
if(Input.GetKey(KeyCode.Q) || (Input.GetAxis("Mouse ScrollWheel") < 0))
{
zoom -= 1;
zoom = Mathf.Max(zoom_bound_min, Mathf.Min(zoom_bound_max, zoom));
}
if(Input.GetKey(KeyCode.E) || (Input.GetAxis("Mouse ScrollWheel") > 0))
{
zoom += 1;
zoom = Mathf.Max(zoom_bound_min, Mathf.Min(zoom_bound_max, zoom));
}
}
}
This is full of bugs and doesn’t work as provided in JS
Hi!
Thanks for your code… but, unfortunately this code it’s incomplete… could you update this code?
Thanks
Well its meant as educational, not take and use in production, everything you will find here is pretty much like that. Not that I am not finishing what I started but for instance, this camera code could be used “as is”. When you mean incomplete, you could be referencing any game and you would be right.
Great writing style, I love to make information since the comments encourages writers for being more engaged and towards the opportunity to certainly know from each other.
What victor was referring to is that the code above has had all of the asterisks taken out by the code formatter. They may not be valid HTML entities or the encoding of the page isn’t kosher.
Any time there is an error in the above code just input an asterisk before the unexpected variable. That should fix the issues.
Thanks for writing such a thought-provoking post. Let’s keep the comments relevant.