Created
September 6, 2016 05:09
-
-
Save pointcache/8f6d3b07d87ef6488c5c085f723f052d to your computer and use it in GitHub Desktop.
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 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