Skip to content

Instantly share code, notes, and snippets.

@pointcache
Created September 6, 2016 05:09
Show Gist options
  • Save pointcache/8f6d3b07d87ef6488c5c085f723f052d to your computer and use it in GitHub Desktop.
Save pointcache/8f6d3b07d87ef6488c5c085f723f052d to your computer and use it in GitHub Desktop.
using UnityEngine;
using System;
using System.Collections.Generic;
public class ParentConstraint : MonoBehaviour
{
public UpdateMethod updateMethod;
public enum UpdateMethod
{
update,
lateUpdate,
dontUpdate
}
private Vector3 childLocalPos;
private Quaternion _childRotOffset;
public Transform _parent;
void OnEnable()
{
childLocalPos = _parent.InverseTransformPoint(transform.position);
_childRotOffset = Quaternion.FromToRotation(_parent.forward, transform.forward);
}
void Update()
{
if (updateMethod != UpdateMethod.update)
return;
Sync();
}
public void Sync()
{
transform.position = _parent.TransformPoint(childLocalPos);
transform.rotation = _parent.rotation * _childRotOffset;
}
void LateUpdate()
{
if (updateMethod != UpdateMethod.lateUpdate)
return;
Sync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment