Skip to content

Instantly share code, notes, and snippets.

@pjnovas
Created July 30, 2017 02:17
Show Gist options
  • Save pjnovas/007328535387fc948007a3e5ac8995cf to your computer and use it in GitHub Desktop.
Save pjnovas/007328535387fc948007a3e5ac8995cf to your computer and use it in GitHub Desktop.
#pragma strict
var target : Transform; // Reference to the Objects transform.
var targetBody: Rigidbody2D;
var distance: float = 3.0;
var height: float = 3.0;
var width: float = 3.0;
var damping: float = 5.0;
var zoomSpeed: float = 1;
var zoomMax: float = 10;
var zoomMin: float = 5;
var zooming: boolean;
var thisCamera: Camera;
var isMain: System.Boolean;
private var cameraMin: Vector2 = new Vector2(-26, -26);
private var cameraMax: Vector2 = new Vector2(26, 26);
private var tLerp: float = 0;
private var tLerp2: float = 0;
private var halfH: float;
private var halfW: float;
function Start(){
if (isMain){
var windBlow = gameObject.AddComponent(AudioSource);
windBlow.clip = GameSounds.current.windBlow;
windBlow.volume = GameSounds.current.volumeOf("windBlow");
windBlow.loop = true;
windBlow.Play();
}
}
function Update(){
tLerp += Time.deltaTime * zoomSpeed;
tLerp2 += Time.deltaTime * damping;
if (zooming){
var zoom = Mathf.Clamp(targetBody.velocity.magnitude * 2, zoomMin, zoomMax);
thisCamera.orthographicSize = Mathf.Lerp(thisCamera.orthographicSize, zoom, tLerp);
}
var cheight = 2 * thisCamera.orthographicSize;
var cwidth = cheight * thisCamera.aspect;
halfH = cheight/2f;
halfW = cwidth/2f;
var wantedPosition: Vector3 = target.TransformPoint(width, height, -distance);
if (wantedPosition.x - halfW < cameraMin.x){
wantedPosition.x = cameraMin.x + halfW;
}
if (wantedPosition.x + halfW > cameraMax.x){
wantedPosition.x = cameraMax.x - halfW;
}
if (wantedPosition.y - halfH < cameraMin.y){
wantedPosition.y = cameraMin.y + halfH;
}
if (wantedPosition.y + halfH > cameraMax.y){
wantedPosition.y = cameraMax.y - halfH;
}
transform.position = Vector3.Lerp(transform.position, wantedPosition, tLerp2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment