Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created April 4, 2023 00:58
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/1a4d2faa2f90ace9aa9aa84db0e5842c to your computer and use it in GitHub Desktop.
Save kurtdekker/1a4d2faa2f90ace9aa9aa84db0e5842c to your computer and use it in GitHub Desktop.
chase a parent transform, rotation and position only (not scale)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// @kurtdekker - used to LateUpdate()-chase another
// Transform, position and rotation only.
//
// To use, call the Attach() factory, tell it:
// - who you are that want to chase
// - the target you want to chase
// - how you want to chase it (rotation and/or position)
public class ChaseParent : MonoBehaviour
{
Transform target;
bool chasePosition;
bool chaseRotation;
// go is "me", the distal object that wants to chase something
// target is "they," the Transform I am chasing
public static ChaseParent Attach(
GameObject go,
Transform target,
bool chasePosition = true,
bool chaseRotation = true)
{
ChaseParent chase = go.AddComponent<ChaseParent>();
chase.target = target;
chase.chasePosition = chasePosition;
chase.chaseRotation = chaseRotation;
return chase;
}
void LateUpdate ()
{
if (chasePosition)
{
transform.position = target.position;
}
if (chaseRotation)
{
transform.rotation = target.rotation;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment