Created
September 10, 2015 18:07
-
-
Save SnugglePilot/81e4716ddc1f6b36a20f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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