Skip to content

Instantly share code, notes, and snippets.

@donfreiday
Created March 5, 2020 18:25
Show Gist options
  • Save donfreiday/923ad796b4ec8489e2ad1df6412f7fce to your computer and use it in GitHub Desktop.
Save donfreiday/923ad796b4ec8489e2ad1df6412f7fce to your computer and use it in GitHub Desktop.
// Attach to camera to have it follow a GameObject in Unity 2d
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCharacter : MonoBehaviour
{
public float interpolationSpeed = 0.5f;
public float verticalOffset = 4.0f;
public GameObject focusObject;
void Start()
{
focusObject = GameObject.Find("Mario");
}
void LateUpdate()
{
Vector3 targetPosition = new Vector3(
focusObject.transform.position.x,
focusObject.transform.position.y + verticalOffset,
transform.position.z);
transform.position = Vector3.Lerp(transform.position, targetPosition, interpolationSpeed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment