Skip to content

Instantly share code, notes, and snippets.

@mnogue
Created January 13, 2019 20:43
Show Gist options
  • Save mnogue/e2f029537e5985e7e864588cd9bedd05 to your computer and use it in GitHub Desktop.
Save mnogue/e2f029537e5985e7e864588cd9bedd05 to your computer and use it in GitHub Desktop.
Custom frame-per-second 3D animations for Unity3D. Based on Spider-Man: Into the Spider-Verse movie
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ToonMotion : MonoBehaviour
{
private class Snapshot
{
public Transform transform;
public Vector3 position;
public Quaternion rotation;
public Vector3 scale;
public Snapshot(Transform transform)
{
this.transform = transform;
this.Update();
}
public void Update()
{
this.position = this.transform.position;
this.rotation = this.transform.rotation;
this.scale = this.transform.localScale;
}
}
private Dictionary<int, Snapshot> snapshots = new Dictionary<int, Snapshot>();
private float updateTime = 0f;
[Range(1, 60)] public int fps = 20;
private void LateUpdate()
{
if (Time.time - this.updateTime > 1f/this.fps)
{
this.SaveSnapshot(transform);
this.updateTime = Time.time;
}
foreach(KeyValuePair<int, Snapshot> item in this.snapshots)
{
if (item.Value.transform != null)
{
item.Value.transform.position = item.Value.position;
item.Value.transform.rotation = item.Value.rotation;
item.Value.transform.localScale = item.Value.scale;
}
}
}
private void SaveSnapshot(Transform parent)
{
if (parent == null) return;
int childrenCount = parent.childCount;
for (int i = 0; i < childrenCount; ++i)
{
Transform target = parent.GetChild(i);
int uid = target.GetInstanceID();
this.snapshots[uid] = new Snapshot(target);
this.SaveSnapshot(target);
}
}
}
@Unshackled-Games
Copy link

That is awesome work. Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment