Skip to content

Instantly share code, notes, and snippets.

@mduheaume
Created January 29, 2012 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mduheaume/1700534 to your computer and use it in GitHub Desktop.
Save mduheaume/1700534 to your computer and use it in GitHub Desktop.
CamerController
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
Vector3 worldUp=new Vector3(0f, 0f, 0f);
public GameObject target;
public int CameraMode = 1;
void Start () {
}
void Awake() {
this.target=GameObject.FindWithTag("target");
}
void Update () {
if (!this.target) {
this.target=GameObject.FindWithTag("target");
}
switch (this.CameraMode) {
case 1:
UpdateMode1();
break;
case 2:
UpdateMode2();
break;
default:
UpdateMode2();
break;
}
}
void UpdateMode2() {
float tz = 0f+this.target.transform.position.z-5;
if (tz>0) tz=0;
float ty = 15f+(tz*0.5f);
Vector3 wantedPosition = new Vector3(0, ty, tz);
this.transform.position = Vector3.Lerp(this.transform.position, wantedPosition, Time.time * 0.0001f);
Quaternion wantedRotation = Quaternion.LookRotation(this.target.transform.position - this.transform.position, worldUp);
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, wantedRotation, Time.time * 0.0001f);
}
void UpdateMode1() {
float dy = Mathf.Abs(this.target.transform.position.z*2);
Vector3 movingTo = new Vector3(0, 15f-(0.5f*dy), -1.5f*dy);
this.transform.position-=(this.transform.position-movingTo)/20;
this.transform.LookAt(this.target.transform, worldUp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment