You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.6 KiB
104 lines
2.6 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using uc.Spline;
|
|
using UnityEngine;
|
|
|
|
public class IronHorse : MonoBehaviour {
|
|
|
|
[SerializeField]
|
|
private IronHorseDataHolder ironHorseHolder;
|
|
|
|
public BezierSplineComponent m_bezierSpline;
|
|
[SerializeField]
|
|
public float targetSpeed;
|
|
public Ease.Easing m_ease;
|
|
public bool playOnStart = true;
|
|
|
|
public float speedSmooth { set { speed_smooth = value; } }
|
|
private float speed_smooth = 0.1f;
|
|
float m_speed = 1;
|
|
float m_position = 0;
|
|
BaseSpline.SplineIterator m_iter;
|
|
|
|
public float initialPosition = 0;
|
|
|
|
public float Progress
|
|
{
|
|
get { if(m_bezierSpline != null) return m_position / m_bezierSpline.Spline.Length; return 0; }
|
|
}
|
|
|
|
// Use this for initialization
|
|
IEnumerator Start ()
|
|
{
|
|
while (m_bezierSpline == null)
|
|
yield return null;
|
|
|
|
m_iter = m_bezierSpline.Spline.GetIterator();
|
|
m_iter.SetTransform(m_bezierSpline.transform);
|
|
|
|
if (ironHorseHolder)
|
|
{
|
|
targetSpeed = ironHorseHolder.TargetSpeed;
|
|
speed_smooth = ironHorseHolder.SpeedSmooth;
|
|
m_ease = ironHorseHolder.ease;
|
|
}
|
|
if(initialPosition != 0)
|
|
m_position = initialPosition;
|
|
|
|
|
|
if (playOnStart)
|
|
Play();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (m_bezierSpline == null || m_iter == null)
|
|
return;
|
|
|
|
m_speed = Mathf.Lerp(m_speed, targetSpeed, speed_smooth);
|
|
|
|
if(isPlaying)
|
|
m_position += m_speed * Time.deltaTime;
|
|
|
|
float position = Ease.EaseByType(m_ease, 0, m_bezierSpline.Spline.Length, m_position / m_bezierSpline.Spline.Length);
|
|
m_iter.SetOffset(position);
|
|
|
|
transform.position = m_iter.GetPosition();
|
|
|
|
Vector3 tangent = m_iter.GetTangent();
|
|
if(tangent != Vector3.zero)
|
|
transform.localRotation = Quaternion.LookRotation(tangent);
|
|
}
|
|
|
|
bool isPlaying = false;
|
|
public void Play()
|
|
{
|
|
isPlaying = true;
|
|
}
|
|
|
|
public void Pause()
|
|
{
|
|
isPlaying = false;
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
Pause();
|
|
m_position = initialPosition;
|
|
}
|
|
|
|
void OnDrawGizmos()
|
|
{
|
|
if (m_iter == null)
|
|
return;
|
|
Gizmos.matrix = transform.localToWorldMatrix;
|
|
Gizmos.color = Color.black;
|
|
float fov = 30;
|
|
float near = 0;
|
|
float far = 10;
|
|
float aspect = 2;
|
|
Gizmos.DrawFrustum(Vector3.zero, fov, far, near, aspect);
|
|
Gizmos.DrawLine(Vector3.zero, Vector3.forward * far);
|
|
}
|
|
}
|
|
|