Skip to content

Instantly share code, notes, and snippets.

@Curookie
Created December 18, 2017 10:22
Show Gist options
  • Save Curookie/8b3f1dec1aa1b8634127f885fb2ca54e to your computer and use it in GitHub Desktop.
Save Curookie/8b3f1dec1aa1b8634127f885fb2ca54e to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraCtrl : MonoBehaviour {
public Transform target; // The position that that camera will be following.
public float smoothing = 5f; // The speed with which the camera will be following.
Vector3 offset; // The initial offset from the target.
void Start()
{
// Calculate the initial offset.
offset = transform.position - target.position;
}
void FixedUpdate()
{
// Create a postion the camera is aiming for based on the offset from the target.
Vector3 targetCamPos = target.position + offset;
// Smoothly interpolate between the camera's current position and it's target position.
transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment