Files
cppk-ar/Assets/Scripts/InputHandler.cs
T
2022-11-12 06:11:51 +05:00

68 lines
1.4 KiB
C#

using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputHandler : MonoBehaviour, IGameComponent
{
enum Axis
{
x,
y,
z,
w
}
private Transform cameraTransform;
private float X0;
private float P0;
private readonly float F = 1;
private readonly float Q = 6;
private readonly float H = 1;
private readonly float R = 15;
private float Covariance = .1f;
private float[] state = new float[4];
float GetValueAfterFilter(float value, Axis axis)
{
X0 = F * state[(int)axis];
P0 = F * Covariance * F + Q;
var K = H *P0 / (H * P0 * H + R);
state[(int)axis] = X0 + K * (value - H * X0);
Covariance = (1 - K * H) * P0;
return state[(int)axis];
}
void Awake()
{
cameraTransform = transform;
}
void Start()
{
GameSystem.Instance.AddGameComponent(this);
Input.gyro.enabled = true;
}
public void OnUpdate()
{
var phoneRotation = Input.gyro.attitude;
cameraTransform.rotation = phoneRotation;
cameraTransform.Rotate(0, 0, 180, Space.Self);
cameraTransform.Rotate(90, 180, 0, Space.World);
}
public void OnFixedUpdate() { }
public void OnLateUpdate() { }
public void OnDestroy()
{
GameSystem.Instance.RemoveGameComponent(this);
}
}