Skip to content

Instantly share code, notes, and snippets.

@alfchee
Created September 25, 2015 23:14
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 alfchee/068712aee8c6cbbac101 to your computer and use it in GitHub Desktop.
Save alfchee/068712aee8c6cbbac101 to your computer and use it in GitHub Desktop.
Is a Third Person Camera that allows to brings to the Main Camera of an scene in Unity3D to follow the main character that have a lookAt object in it's hierarchy
using UnityEngine;
using System.Collections;
public class TP_Camera : MonoBehaviour
{
public static TP_Camera Instance;
public Transform TargetLookAt;
public float Distance = 5f;
public float DistanceMin = 3f;
public float DistanceMax = 10f;
public float DistanceSmooth = 0.05f;
public float X_MouseSensitivity = 5f;
public float Y_MouseSensitivity = 5f;
public float MouseWheelSensitivity = 5f;
public float X_Smooth = 0.05f;
public float Y_Smooth = 0.1f;
public float Y_MinLimit = -40f;
public float Y_MaxLimit = 80f;
private float mouseX = 0;
private float mouseY = 0;
private float velX = 0;
private float velY = 0;
private float velZ = 0;
private float velDistance = 0;
private float startDistance = 0;
private Vector3 position = Vector3.zero;
private Vector3 desiredPositon = Vector3.zero;
private float desiredDistance = 0;
void Awake()
{
Instance = this;
Debug.Log ("Awake of TP_Camera");
}//Awake()
// Use this for initialization
void Start()
{
Distance = Mathf.Clamp (Distance, DistanceMin, DistanceMax);
startDistance = Distance;
Reset ();
}//Start()
// Update is called once per frame
void LateUpdate()
{
if (TargetLookAt == null) {
return;
}
HandlePlayerInput ();
CalculateDesiredPosition ();
UpdatePosition ();
}//Update()
void HandlePlayerInput()
{
var deadZone = 0.01f;
// The right mouse button is down
if (Input.GetMouseButton (1)) {
mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity;
mouseY += Input.GetAxis("Mouse Y") * Y_MouseSensitivity;
}
// clamp the mouse rotation in Y axis
mouseY = Helper.ClampAngle (mouseY, Y_MinLimit, Y_MaxLimit);
if (Input.GetAxis ("Mouse ScrollWheel") < -deadZone ||
Input.GetAxis ("Mouse ScrollWheel") > deadZone) {
desiredDistance = Mathf.Clamp(Distance - Input.GetAxis ("Mouse ScrollWheel") * MouseWheelSensitivity, DistanceMin, DistanceMax);
}
}//HandlePlayerInput()
void CalculateDesiredPosition()
{
// Eval our distance
Distance = Mathf.SmoothDamp (Distance, desiredDistance, ref velDistance, DistanceSmooth);
// get desired position
desiredPositon = CalculatePosition (mouseY, mouseX, Distance);
}//CalculateDesiredPosition()
Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
{
Vector3 direction = new Vector3 (0, 0, -distance);
Quaternion rotation = Quaternion.Euler (rotationX, rotationY, 0);
return TargetLookAt.position + rotation * direction;
}//CalculatePosition()
void UpdatePosition()
{
// smooth x, y and z
var posX = Mathf.SmoothDamp (position.x, desiredPositon.x, ref velX, X_Smooth);
var posY = Mathf.SmoothDamp (position.y, desiredPositon.y, ref velY, Y_Smooth);
var posZ = Mathf.SmoothDamp (position.z, desiredPositon.z, ref velZ, X_Smooth);
position = new Vector3 (posX, posY, posZ);
// change the position of the camera
transform.position = position;
// look at the target
transform.LookAt (TargetLookAt);
}//UpdatePosition()
public void Reset()
{
mouseX = 0;
mouseY = 10f;
Distance = startDistance;
desiredDistance = Distance;
}//Reset()
public static void UseExistingOrCreateNewMainCamera()
{
GameObject tempCamera, targetLookAt;
TP_Camera myCamera;
if (Camera.main != null) {
tempCamera = Camera.main.gameObject;
} else {
tempCamera = new GameObject("Main Camera");
tempCamera.AddComponent<Camera>();
tempCamera.tag = "MainCamera";
}
tempCamera.AddComponent <TP_Camera>();
myCamera = tempCamera.GetComponent ("TP_Camera") as TP_Camera;
targetLookAt = GameObject.Find ("targetLookAt") as GameObject;
if (targetLookAt == null) {
targetLookAt = new GameObject("targetLookAt");
targetLookAt.transform.position = Vector3.zero;
}
myCamera.TargetLookAt = targetLookAt.transform;
}//UseExistingOrCreateNewMainCamera()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment