Unity3D – Fez Style Camera Pan

Put the following script on your camera, setup your 4 waypoints on each “side” of your map, press space and there you go! Make sure to make the camera Orthographic to get the desired effect.

using UnityEngine;
using System.Collections;

public class RotateCamera : MonoBehaviour {

  public float smoothTime = 0.5f;
  public GameObject[] waypoints;
  public int currentWaypoint = 0;

  private bool isChanging = false;

  
  // Update is called once per frame
  void Update () {

    if(currentWaypoint > 3) {
      currentWaypoint = 0;
    }

    if(Input.GetKeyDown(KeyCode.Space) ) {
      if(isChanging) {
        return;
      } else {
        isChanging = true;
        StartCoroutine(DoIt());
        currentWaypoint++;
      }
    }
  }

  IEnumerator DoIt () {
    float i = 0;
    while(i <= 1) {
      i += Time.deltaTime * smoothTime;
      transform.position = Vector3.Lerp(transform.position, waypoints[currentWaypoint].transform.position, i);
      switch(currentWaypoint) {
      case 0:
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, 0), i);
        break;
      case 1:
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, -90, 0), i);
        break;
      case 2:
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 180, 0), i);
        break;
      case 3:
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 90, 0), i);
        break;
      }
      yield return null;
    }
    isChanging = false;
  }
}

facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>