84 lines
2.1 KiB
C#
84 lines
2.1 KiB
C#
using OctoCore.Components;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class Door : MonoBehaviour
|
|
{
|
|
[System.Serializable]
|
|
public class References
|
|
{
|
|
public Transform HingeForward;
|
|
public Transform HingeBackward;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class Events
|
|
{
|
|
public UnityEvent OnDoorOpen;
|
|
public UnityEvent OnDoorClose;
|
|
}
|
|
|
|
[SerializeField] private References _references;
|
|
[SerializeField] private Events _events;
|
|
|
|
private bool _opened;
|
|
private float _rot;
|
|
private Vector3 _virtualPos;
|
|
|
|
void OnDrawGizmos()
|
|
{
|
|
Gizmos.color = Color.green;
|
|
Gizmos.matrix = transform.localToWorldMatrix;
|
|
Gizmos.DrawWireCube(_virtualPos, Vector3.one * .1f);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!OctoPlayer.Instance)
|
|
return;
|
|
|
|
Vector3 p = transform.InverseTransformPoint(OctoPlayer.Instance.transform.position);
|
|
|
|
float dMin = OctoPlayer.Instance.Radius * 2;
|
|
float dMax = dMin * 3f;
|
|
|
|
float d0 = Vector3.Distance(_virtualPos, p);
|
|
float d1 = Mathf.Abs(p.z);
|
|
|
|
if (d0 < dMin && d1 < dMax)
|
|
{
|
|
_virtualPos.z = p.z + dMin * (_virtualPos.z < p.z ? -1 : 1);
|
|
}
|
|
else
|
|
{
|
|
_virtualPos = Vector3.Lerp(_virtualPos, Vector3.zero, Time.deltaTime * 3);
|
|
}
|
|
|
|
float rot = Mathf.Clamp(_virtualPos.z / dMin, -1, 1) * 95f;
|
|
|
|
_rot = Mathf.Lerp(_rot, rot, Time.deltaTime * 5f);
|
|
|
|
if (_rot < 0)
|
|
{
|
|
_references.HingeBackward.localEulerAngles = new Vector3(0, _rot, 0);
|
|
_references.HingeForward.localEulerAngles = Vector3.zero;
|
|
}
|
|
else
|
|
{
|
|
_references.HingeBackward.localEulerAngles = Vector3.zero;
|
|
_references.HingeForward.localEulerAngles = new Vector3(0, _rot, 0);
|
|
}
|
|
|
|
if (!_opened && Mathf.Abs(_virtualPos.z) > .04f)
|
|
{
|
|
_opened = true;
|
|
_events.OnDoorOpen?.Invoke();
|
|
}
|
|
else if (_opened && Mathf.Abs(_virtualPos.z) < .02f)
|
|
{
|
|
_opened = false;
|
|
_events.OnDoorClose?.Invoke();
|
|
}
|
|
}
|
|
}
|