62 lines
1.8 KiB
C#
62 lines
1.8 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
[ExecuteInEditMode]
|
|
public class MegaWalkBridge : MonoBehaviour
|
|
{
|
|
public GameObject bridge;
|
|
|
|
[HideInInspector]
|
|
public MegaCurveDeform mod;
|
|
|
|
public float offset;
|
|
|
|
public float smooth;
|
|
|
|
private void Update()
|
|
{
|
|
if (!bridge)
|
|
{
|
|
return;
|
|
}
|
|
if (mod == null)
|
|
{
|
|
mod = bridge.GetComponent<MegaCurveDeform>();
|
|
}
|
|
if ((bool)mod)
|
|
{
|
|
int axis = (int)mod.axis;
|
|
Vector3 position = base.transform.position;
|
|
Vector3 point = mod.transform.worldToLocalMatrix.MultiplyPoint(position);
|
|
if (point.x > mod.bbox.min.x && point.x < mod.bbox.max.x && point.z > mod.bbox.min.z && point.z < mod.bbox.max.z)
|
|
{
|
|
float alpha = (point[axis] - mod.bbox.min[axis]) / (mod.bbox.max[axis] - mod.bbox.min[axis]);
|
|
SetPos(mod, alpha);
|
|
point.y = mod.GetPos(alpha) + offset * 0.01f;
|
|
base.transform.position = bridge.transform.localToWorldMatrix.MultiplyPoint(point);
|
|
}
|
|
else
|
|
{
|
|
SetPos(mod, 0f);
|
|
}
|
|
}
|
|
}
|
|
|
|
private float easeInOutSine(float start, float end, float value)
|
|
{
|
|
end -= start;
|
|
return (0f - end) / 2f * (Mathf.Cos((float)Math.PI * value / 1f) - 1f) + start;
|
|
}
|
|
|
|
public void SetPos(MegaCurveDeform mod, float alpha)
|
|
{
|
|
float num = 0f;
|
|
num = ((!(alpha < 0.5f)) ? easeInOutSine((0f - mod.MaxDeviation) * 0.01f, 0f, (alpha - 0.5f) / 0.5f) : easeInOutSine(0f, (0f - mod.MaxDeviation) * 0.01f, alpha / 0.5f));
|
|
mod.SetKey(0, 0f, 0f, Mathf.Tan(0f), Mathf.Tan(Mathf.Atan2(num, alpha)));
|
|
float intan = Mathf.Lerp(Mathf.Tan(0f), Mathf.Tan(Mathf.Atan2(num, alpha)), smooth);
|
|
float outtan = Mathf.Lerp(Mathf.Tan((float)Math.PI), Mathf.Tan(Mathf.Atan2(num, alpha - 1f)), smooth);
|
|
mod.SetKey(1, Mathf.Clamp(alpha, 0.001f, 0.999f), num, intan, outtan);
|
|
mod.SetKey(2, 1f, 0f, Mathf.Tan(Mathf.Atan2(0f - num, 1f - alpha)), Mathf.Tan((float)Math.PI));
|
|
}
|
|
}
|