Last active
December 30, 2016 23:22
-
-
Save ipha/05303546a2ef409b191c32114282a66d 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.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