Created
April 4, 2023 00:58
-
-
Save kurtdekker/1a4d2faa2f90ace9aa9aa84db0e5842c to your computer and use it in GitHub Desktop.
chase a parent transform, rotation and position only (not scale)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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