Skip to content

Instantly share code, notes, and snippets.

@IRobS
Created August 27, 2019 08:37
Show Gist options
  • Save IRobS/4f2a41c4b0cfe2fdb105f60ba28d5b2c to your computer and use it in GitHub Desktop.
Save IRobS/4f2a41c4b0cfe2fdb105f60ba28d5b2c to your computer and use it in GitHub Desktop.
A simple 3rd person chase camera
/**
* A simple 3rd person chase camera
*/
background: #3b8;
min-height: 100%;
<pre>
<code>
using UnityEngine;
using System.Collections;
public class SmoothFollow2 : MonoBehaviour
{
public Transform target;
public float distance = 3.0f;
public float height = 3.0f;
public float damping = 5.0f;
public bool smoothRotation = true;
public bool followBehind = true;
public float rotationDamping = 10.0f;
void Update()
{
Vector3 wantedPosition;
if (followBehind)
wantedPosition = target.TransformPoint(0, height, -distance);
else
wantedPosition = target.TransformPoint(0, height, distance);
transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation)
{
Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else transform.LookAt(target, target.up);
}
}
</code>
</pre>
// alert('Hello world!');
{"view":"split","fontsize":"100","seethrough":"","prefixfree":"1","page":"all"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment