Skip to content

Instantly share code, notes, and snippets.

@korchoon
Created March 31, 2020 09:43
Show Gist options
  • Save korchoon/96124033257c486fcf56ac7bd08fa342 to your computer and use it in GitHub Desktop.
Save korchoon/96124033257c486fcf56ac7bd08fa342 to your computer and use it in GitHub Desktop.
public static class PoseUtils {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Pose GetPose(this Transform transform) {
return new Pose() {position = transform.position, rotation = transform.rotation};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetPose(this Transform transform, Pose pose) {
transform.position = pose.position;
transform.rotation = pose.rotation;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Pose With(this Pose src, Vector3? position = default, Quaternion? rotation = default) {
src.position = position ?? src.position;
src.rotation = rotation ?? src.rotation;
return src;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Pose PoseLerp(Pose start, Pose end, float progress) {
var pos = Vector3.Lerp(start.position, end.position, progress);
var rot = Quaternion.Slerp(start.rotation, end.rotation, progress);
return new Pose(pos, rot);
}
public static async Routine Blend(Transform agent, float timeTotal, AnimationCurve curve, Pose start, Pose end) {
var endTime = Time.time + timeTotal;
while (true) {
var linear = endTime.Progress01(Time.time, timeTotal);
if (linear >= 1f) {
agent.SetPose(PoseLerp(start, end, curve.Evaluate(1f)));
break;
}
var curved = curve.Evaluate(linear);
agent.SetPose(PoseLerp(start, end, curved));
await Sch.Update;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment