Last active
December 30, 2016 20:37
-
-
Save theawless/042d57d6bc4ca9ceab13557e63aae189 to your computer and use it in GitHub Desktop.
Useful for fighting games like Mortal Kombat, Street Fighters, Tekken. This camera looks at the plane created by the line joining the two targets and vertical up
This file contains 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; | |
namespace Assets.Standard_Assets.Cameras.Scripts | |
{ | |
// Useful for fighting games like Mortal Kombat, Street Fighters, Tekken | |
// This camera looks at the plane created by the line joining the two targets and vertical up | |
public class PlaneCamera : MonoBehaviour | |
{ | |
[SerializeField] | |
private bool autoTarget = false; | |
[SerializeField] | |
private string target1Tag = "Player1"; | |
[SerializeField] | |
private string target2Tag = "Player2"; | |
[SerializeField] | |
private float minimumZoom = 3f; | |
[SerializeField] | |
private float maximumZoom = 7f; | |
[SerializeField] | |
private float zoomMultiplier = 1f; // works well between 1f to 2f, use for increasing zoom effect | |
[SerializeField] | |
private float heightOffset = 1.5f; // height from the ground | |
[SerializeField] | |
private bool smoothFollow = true; | |
[SerializeField] | |
private bool smoothLook = true; | |
[SerializeField] | |
private float lookSpeed = 0.03f; // damping effect to prevent jittering, used when targets move left/right | |
[SerializeField] | |
private float followSpeed = 0.05f; // damping effect to prevent jittering, used when targets move in in/out | |
[SerializeField] | |
private Transform target1 = null; | |
[SerializeField] | |
private Transform target2 = null; | |
void FindTargets() | |
{ | |
if (target1 == null) | |
target1 = GameObject.FindGameObjectWithTag(target1Tag).transform; | |
if (target2 == null) | |
target2 = GameObject.FindGameObjectWithTag(target2Tag).transform; | |
} | |
// LateUpdate because we need to track objects which have moved in Update | |
// Position the camera perpendicular to the line that connects the targets and Vertical up | |
// Make the camera look at the mid point of the targets | |
void LateUpdate() | |
{ | |
if (autoTarget) | |
FindTargets(); | |
if (target1 == null || target2 == null) | |
return; // nothing to do | |
var midPoint = (target2.position + target1.position) / 2; | |
var lineVector = target2.position - target1.position; // this is the straight line that joins the targets | |
var perToLine = Vector3.Cross(lineVector, Vector3.up); // this is perpendicular to the above line | |
perToLine = perToLine * zoomMultiplier; | |
perToLine = perToLine.normalized * Mathf.Clamp(perToLine.magnitude, minimumZoom, maximumZoom); // make sure it is in desired range | |
var newPosition = midPoint - perToLine; | |
newPosition.y += heightOffset; // to prevent the camera being really close to ground | |
// depending on whether smoothing is required, update the camera position and rotation | |
transform.position = smoothFollow ? Vector3.Lerp(transform.position, newPosition, followSpeed) : newPosition; | |
transform.rotation = smoothLook ? Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(perToLine), lookSpeed) : Quaternion.LookRotation(midPoint); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment