Build TravelRun intro and tutorial flow
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class FlowIntroRouteMotion : MonoBehaviour
|
||||
{
|
||||
public Vector2[] points;
|
||||
public Vector2 offset;
|
||||
public float duration = 7.5f;
|
||||
|
||||
private RectTransform rectTransform;
|
||||
private Vector3 baseScale = Vector3.one;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
rectTransform = GetComponent<RectTransform>();
|
||||
if(rectTransform != null)
|
||||
{
|
||||
baseScale = rectTransform.localScale;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if(rectTransform == null)
|
||||
{
|
||||
rectTransform = GetComponent<RectTransform>();
|
||||
}
|
||||
|
||||
if(rectTransform != null)
|
||||
{
|
||||
baseScale = rectTransform.localScale;
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if(rectTransform == null || points == null || points.Length < 2 || duration <= 0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
float totalLength = GetTotalLength();
|
||||
if(totalLength <= 0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
float distance = Mathf.Repeat(Time.unscaledTime / duration, 1f) * totalLength;
|
||||
Vector2 position = points[0];
|
||||
Vector2 direction = Vector2.right;
|
||||
|
||||
for(int i = 0; i < points.Length - 1; i++)
|
||||
{
|
||||
Vector2 from = points[i];
|
||||
Vector2 to = points[i + 1];
|
||||
Vector2 delta = to - from;
|
||||
float segmentLength = delta.magnitude;
|
||||
|
||||
if(distance <= segmentLength)
|
||||
{
|
||||
float t = segmentLength > 0f ? distance / segmentLength : 0f;
|
||||
position = Vector2.Lerp(from, to, t);
|
||||
direction = delta.normalized;
|
||||
break;
|
||||
}
|
||||
|
||||
distance -= segmentLength;
|
||||
}
|
||||
|
||||
rectTransform.anchoredPosition = position + offset;
|
||||
|
||||
if(direction.sqrMagnitude > 0.0001f)
|
||||
{
|
||||
Vector2 displayDirection = direction;
|
||||
Vector3 displayScale = baseScale;
|
||||
|
||||
if(displayDirection.x < 0f)
|
||||
{
|
||||
displayDirection.x = -displayDirection.x;
|
||||
displayScale.x = -Mathf.Abs(baseScale.x);
|
||||
}
|
||||
else
|
||||
{
|
||||
displayScale.x = Mathf.Abs(baseScale.x);
|
||||
}
|
||||
|
||||
rectTransform.localScale = displayScale;
|
||||
rectTransform.localRotation = Quaternion.Euler(0f, 0f, Mathf.Atan2(displayDirection.y, displayDirection.x) * Mathf.Rad2Deg - 12f);
|
||||
}
|
||||
}
|
||||
|
||||
private float GetTotalLength()
|
||||
{
|
||||
float totalLength = 0f;
|
||||
|
||||
for(int i = 0; i < points.Length - 1; i++)
|
||||
{
|
||||
totalLength += Vector2.Distance(points[i], points[i + 1]);
|
||||
}
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user