Files
taptap2024_GJ_chidouren/Assets/Scripts/Game/Component/GyroscopeController/GyroscopeEulerController.cs
2024-10-16 00:03:41 +08:00

38 lines
1.2 KiB
C#

using System;
using Framework.Utils.Extend;
using UnityEngine;
namespace Game.Component
{
public class GyroscopeEulerController : MonoBehaviour
{
[SerializeField] private Vector3 _minGravity;
[SerializeField] private Vector3 _maxGravity;
[SerializeField] private float _offset;
private Vector3 _curGravity;
private Vector3 _baseRotation;
private Vector3 _baseGravity;
void OnEnable ()
{
this._baseRotation = this.transform.localEulerAngles;
this._baseGravity = GyroscopeMgr.Instance.Gyroscope.gravity;
GyroscopeMgr.Instance.AddUpdater (DoUpdate);
}
void DoUpdate (Vector3 gravity)
{
var gyroGravity = gravity - this._baseGravity;
this._curGravity
= Vector3.Slerp (this._curGravity , (gyroGravity * this._offset).Clamp (this._minGravity , this._maxGravity) , 0.25f);
this.transform.localEulerAngles = this._baseRotation + new Vector3 (this._curGravity.y , -this._curGravity.x , 0);
}
private void OnDisable ()
{
GyroscopeMgr.Instance.RemoveUpdater (DoUpdate);
}
}
}