Skip to content

Instantly share code, notes, and snippets.

@ipha
Last active December 30, 2016 23:22
Show Gist options
  • Save ipha/05303546a2ef409b191c32114282a66d to your computer and use it in GitHub Desktop.
Save ipha/05303546a2ef409b191c32114282a66d to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class movement : MonoBehaviour {
const int numSamples = 10;
public GameObject head;
public float scalingFactor;
private Vector3 headPrev;
private Vector3[] velocity = new Vector3[numSamples];
// Use this for initialization
void Start () {
headPrev = head.transform.position;
}
// Update is called once per frame
void Update () {
Rotate ();
velocity [0] = (head.transform.position - headPrev) / Time.deltaTime;
if (velocity [0].magnitude > 20) {
Debug.Log ("Velocity outlier; ignoring");
velocity [0] = velocity [1];
}
Vector3 average = Vector3.zero;
for (int i = 0; i < numSamples; i++) {
average += velocity [i];
}
average = average / numSamples;
ApplyMovement (average);
headPrev = head.transform.position;
}
void ApplyMovement(Vector3 average) {
average.y = 0; // Don't fuck with y movement
transform.position += average * average.magnitude * Time.deltaTime * scalingFactor;
}
void Rotate() {
for (int i = numSamples - 1; i > 0; i--) {
velocity [i] = velocity [i - 1];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment