forked from slek120/Unity-Touch-Smoothing-One-Euro-Filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExponentialSmoothing.cs
31 lines (25 loc) · 910 Bytes
/
ExponentialSmoothing.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using UnityEngine;
using System.Collections;
public class ExponentialSmoothing : MonoBehaviour
{
//Smoothing factor from 0 to 1. 1 for no smoothing. 0 for so smooth it doesn't even move.
public float alpha;
private Vector2 currentPosition;
private Vector2 previousFilteredPosition;
void Start ()
{
currentPosition = Camera.main.WorldToScreenPoint (transform.position);
previousFilteredPosition = Camera.main.WorldToScreenPoint (transform.position);
}
void Update ()
{
if (Input.touchCount > 0) {
UnityEngine.Touch touch = Input.GetTouch (0);
currentPosition = touch.position;
}
alpha = Mathf.Clamp (alpha, 0, 1);
Vector2 filteredPosition = alpha * currentPosition + (1 - alpha) * previousFilteredPosition;
previousFilteredPosition = filteredPosition;
transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (filteredPosition.x, filteredPosition.y, 8));
}
}