You've already forked taptap2024_GJ_chidouren
66 lines
2.2 KiB
C#
66 lines
2.2 KiB
C#
using System;
|
|
using Framework.Utils.Extend;
|
|
using Sirenix.OdinInspector;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace Game.Component
|
|
{
|
|
public class GyroscopePositionController : MonoBehaviour
|
|
{
|
|
[SerializeField] private Vector3 _minGravity;
|
|
[SerializeField] private Vector3 _maxGravity;
|
|
[SerializeField] private float _gravityOffset;
|
|
[SerializeField, Range (0, 1)] private float _offset = 0.2f;
|
|
[SerializeField] private bool _isFlip;
|
|
[SerializeField] private bool _isDelay;
|
|
[SerializeField, ShowIf ("_isDelay")] private float _delayTime = 0.75f;
|
|
|
|
private Vector3 _curGravity;
|
|
private Vector3 _basePosition;
|
|
private Vector3 _baseGravity;
|
|
private bool _isUnlock;
|
|
|
|
void OnEnable ()
|
|
{
|
|
if (this._isDelay)
|
|
{
|
|
this._isUnlock = false;
|
|
this.Invoke ("RefreshBasePosition" , this._delayTime);
|
|
}
|
|
else
|
|
{
|
|
RefreshBasePosition ();
|
|
}
|
|
|
|
GyroscopeMgr.Instance.AddUpdater (DoUpdate);
|
|
}
|
|
|
|
private void RefreshBasePosition ()
|
|
{
|
|
this._basePosition = this.transform.localPosition;
|
|
this._baseGravity = GyroscopeMgr.Instance.Gyroscope.gravity;
|
|
this._isUnlock = true;
|
|
}
|
|
|
|
void DoUpdate (Vector3 gravity)
|
|
{
|
|
if (!this._isUnlock)
|
|
{
|
|
return;
|
|
}
|
|
var gyroGravity = (gravity - this._baseGravity) * (this._isFlip ? -1 : 1);
|
|
this._curGravity
|
|
= Vector3.Slerp (this._curGravity , (gyroGravity * this._gravityOffset).Clamp (this._minGravity , this._maxGravity) , 0.25f);
|
|
|
|
this.transform.localPosition = math.lerp (this.transform.localPosition ,
|
|
this._basePosition + new Vector3 (this._curGravity.x , this._curGravity.y , 0) , this._offset);
|
|
}
|
|
|
|
private void OnDisable ()
|
|
{
|
|
GyroscopeMgr.Instance.RemoveUpdater (DoUpdate);
|
|
}
|
|
}
|
|
} |