Isometric Camera: 2D Is 3D, It’s All About Perspective

Isometric View as seen in Starcraft
Isometric View as seen in Starcraft

using UnityEngine;
using System.Collections;

public class IsometricCamera : MonoBehaviour {
    // The View camera 
    public GameObject ViewCamera = null;

    // Camera distance from object
    public float distance = 45.0f;
    // How high up the camera is from object
    public float height = 45.0f;
 
    // Damping for lerping the camera
    public float distanceDamping = 3.0f;
    public float heightDamping = 1.0f;

    void FixedUpdate()
    {

        if (ViewCamera != null)
        {
            // Heights / Distances as defined from above variables
            float wantedHeight = this.transform.position.y + height;
            float wantedDistance = this.transform.position.x + distance;
            float wantedDistanceZ = this.transform.position.z - distance;

            // Current heights / Distances of the object script is attached to
            float currentHeight = transform.position.y;
            float currentDistance = transform.position.x;
            float currentDistanceZ = transform.position.z;

            // Lerp to the wanted height
            currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
            currentDistance = Mathf.Lerp(currentDistance, wantedDistance, distanceDamping * Time.deltaTime);
            currentDistanceZ = Mathf.Lerp(currentDistanceZ, wantedDistanceZ, distanceDamping * Time.deltaTime);

            // Set the camera to the lerp positions and looka at the object
            ViewCamera.transform.position = new Vector3(currentDistance, currentHeight, currentDistanceZ);
            ViewCamera.transform.LookAt(this.gameObject.transform);
        }

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *