Skip to content

Instantly share code, notes, and snippets.

@demonixis
Last active August 29, 2015 14:07
Show Gist options
  • Save demonixis/aa3c202aae89ae3ef946 to your computer and use it in GitHub Desktop.
Save demonixis/aa3c202aae89ae3ef946 to your computer and use it in GitHub Desktop.
CameraFollow script used in my tutorial about Networking with Unity (in French @ http://www.demonixis.net/blog). It's used as a camera component for follow an object.
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
private Transform _transform;
private Quaternion _rotation;
public Transform target;
public Vector3 offset = new Vector3(0, -5, 10);
public float damping = 5;
void Awake()
{
enabled = false;
}
void Start ()
{
_transform = GetComponent<Transform>();
}
void LateUpdate()
{
_rotation = Quaternion.Euler(target.transform.eulerAngles.x, target.transform.eulerAngles.y, 0);
_transform.position = Vector3.Lerp (_transform.position, target.transform.position - (_rotation * offset), Time.deltaTime * damping);
_transform.LookAt(target.transform);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment