Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created November 18, 2021 22:04
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 kurtdekker/1152ceb81a7de7420b796bcca5a86cb9 to your computer and use it in GitHub Desktop.
Save kurtdekker/1152ceb81a7de7420b796bcca5a86cb9 to your computer and use it in GitHub Desktop.
Lets one object follow another object using only a positional offset
using UnityEngine;
// @kurtdekker
// Purpose: follows another Transform, maintaining the same offset from it.
// Useful for a detached health bar that follows a rotating ship (for instance).
public class LateFollow : MonoBehaviour
{
public Transform TargetToFollow;
Vector3 Offset;
void Start ()
{
Offset = transform.position - TargetToFollow.position;
}
void LateUpdate ()
{
transform.position = TargetToFollow.position + Offset;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment