Skip to content

Instantly share code, notes, and snippets.

@SnugglePilot
Created September 10, 2015 18:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SnugglePilot/81e4716ddc1f6b36a20f to your computer and use it in GitHub Desktop.
Save SnugglePilot/81e4716ddc1f6b36a20f to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class LinkedCamera : MonoBehaviour {
// What transform to chase:
public Transform target;
// Debug keyboard controls:
public KeyCode modifier;
public KeyCode incSmooth;
public KeyCode decSmooth;
private Vector3 moveVel;
private Vector3 rotVel;
void Awake() {
moveVel = Vector3.zero;
rotVel = Vector3.zero;
}
void Update () {
if (modifier == KeyCode.None || Input.GetKey (modifier)) {
if (Input.GetKey (incSmooth)) {
Globals.settings.companionCamSmoothingValue += 0.001f;
}
if (Input.GetKey (decSmooth)) {
Globals.settings.companionCamSmoothingValue -= 0.001f;
if (Globals.settings.companionCamSmoothingValue < 0) Globals.settings.companionCamSmoothingValue = 0;
if (Globals.settings.companionCamSmoothingValue < 0) Globals.settings.companionCamSmoothingValue = 0;
}
}
float deltaTime = Time.fixedDeltaTime;
transform.position = Vector3.SmoothDamp(transform.position, target.position, ref moveVel, Globals.settings.companionCamSmoothingValue, Mathf.Infinity, deltaTime);
Vector3 eulerAngles = this.transform.rotation.eulerAngles;
Vector3 targetEulerAngles = target.eulerAngles;
eulerAngles.x = Mathf.SmoothDampAngle(eulerAngles.x, targetEulerAngles.x, ref rotVel.x, Globals.settings.companionCamSmoothingValue, Mathf.Infinity, deltaTime);
eulerAngles.y = Mathf.SmoothDampAngle(eulerAngles.y, targetEulerAngles.y, ref rotVel.y, Globals.settings.companionCamSmoothingValue, Mathf.Infinity, deltaTime);
eulerAngles.z = Mathf.SmoothDampAngle(eulerAngles.z, targetEulerAngles.z, ref rotVel.z, Globals.settings.companionCamSmoothingValue, Mathf.Infinity, deltaTime);
this.transform.rotation = Quaternion.Euler (eulerAngles);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment