Created
December 10, 2014 18:38
-
-
Save thebeardphantom/b07dd0234bbbf68c76c0 to your computer and use it in GitHub Desktop.
This script has some super useful methods for aligning and moving transforms.
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; | |
using UnityEngine; | |
namespace BSGTools.Extensions { | |
public static class TransformExtensions { | |
public static void SetTreePosition(this Transform child, Transform target) { | |
child.root.position = target.position; | |
child.root.position += target.position - child.position; | |
} | |
public static void SetTreeForward(this Transform child, Transform target) { | |
var oldR = child.root.rotation; | |
var newR = Quaternion.LookRotation(target.forward); | |
var t = Quaternion.Inverse(newR) * oldR; | |
child.root.rotation *= t; | |
} | |
public static void SetTreeForwardInverse(this Transform child, Transform target) { | |
var oldR = child.root.rotation; | |
var newR = Quaternion.LookRotation(target.forward); | |
var t = Quaternion.Inverse(oldR) * newR; | |
child.root.rotation *= t; | |
} | |
public static void SetTreeRotation(this Transform child, Transform target) { | |
child.root.rotation = target.rotation; | |
child.root.rotation *= Quaternion.Inverse(child.rotation) * child.root.rotation; | |
} | |
public static void ConnectTo(this Transform child, Transform other) { | |
child.SetTreeForwardInverse(other); | |
child.SetTreePosition(other); | |
} | |
public static void SetPosition(this Transform transform, float? x = null, float? y = null, float? z = null) { | |
Vector3 position = transform.position; | |
if(x.HasValue) | |
position.x = x.Value; | |
if(y.HasValue) | |
position.y = y.Value; | |
if(z.HasValue) | |
position.z = z.Value; | |
transform.position = position; | |
} | |
public static void SetLocalPosition(this Transform transform, float? x = null, float? y = null, float? z = null) { | |
Vector3 localPosition = transform.localPosition; | |
if(x.HasValue) | |
localPosition.x = x.Value; | |
if(y.HasValue) | |
localPosition.y = y.Value; | |
if(z.HasValue) | |
localPosition.z = z.Value; | |
transform.localPosition = localPosition; | |
} | |
public static void SetScale(this Transform transform, float? x = null, float? y = null, float? z = null) { | |
Vector3 scale = transform.localScale; | |
if(x.HasValue) | |
scale.x = x.Value; | |
if(y.HasValue) | |
scale.y = y.Value; | |
if(z.HasValue) | |
scale.z = z.Value; | |
transform.localScale = scale; | |
} | |
public static void SetLocalRotation(this Transform transform, float? x = null, float? y = null, float? z = null) { | |
Vector3 localRotation = transform.localEulerAngles; | |
if(x.HasValue) | |
localRotation.x = x.Value; | |
if(y.HasValue) | |
localRotation.y = y.Value; | |
if(z.HasValue) | |
localRotation.z = z.Value; | |
transform.localRotation = Quaternion.Euler(localRotation); | |
} | |
public static void SetRotation(this Transform transform, float? x = null, float? y = null, float? z = null) { | |
Vector3 rotation = transform.eulerAngles; | |
if(x.HasValue) | |
rotation.x = x.Value; | |
if(y.HasValue) | |
rotation.y = y.Value; | |
if(z.HasValue) | |
rotation.z = z.Value; | |
transform.rotation = Quaternion.Euler(rotation); | |
} | |
public static void ApplyFromCache(this Transform t, CachedTransform tobj) { | |
t.position = tobj.position; | |
t.localPosition = tobj.localPosition; | |
t.rotation = tobj.rotation; | |
t.localRotation = tobj.localRotation; | |
t.localScale = tobj.localScale; | |
} | |
public static CachedTransform MakeCache(this Transform t) { | |
return new CachedTransform(t); | |
} | |
} | |
[Serializable] | |
public struct CachedTransform { | |
public Vector3 position { get; set; } | |
public Vector3 localPosition { get; set; } | |
public Quaternion rotation { get; set; } | |
public Quaternion localRotation { get; set; } | |
public Vector3 lossyScale { get; private set; } | |
public Vector3 localScale { get; set; } | |
public CachedTransform(Transform t) { | |
position = t.position; | |
localPosition = t.localPosition; | |
rotation = t.rotation; | |
localRotation = t.localRotation; | |
lossyScale = t.lossyScale; | |
localScale = t.localScale; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment