WuhuIslandTesting/Library/PackageCache/com.unity.splines@1.0.1/Samples~/Runtime/Rollercoaster.cs

30 lines
716 B
C#
Raw Normal View History

2025-01-07 02:06:59 +01:00
using System;
using System.Collections.Generic;
using Unity.Mathematics;
using Unity.Splines.Examples;
using UnityEngine;
using UnityEngine.Splines;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class Rollercoaster : MonoBehaviour, ISplineProvider
{
[SerializeField]
RollercoasterTrack m_Track;
[SerializeField]
Transform m_Cart;
[SerializeField]
float m_Speed = .314f;
public IEnumerable<Spline> Splines => new[] { m_Track };
void Update()
{
var trs = transform;
var t = math.frac(Time.time * m_Speed);
m_Cart.position = trs.TransformPoint(m_Track.EvaluatePosition(t));
m_Cart.rotation = Quaternion.LookRotation(trs.TransformDirection(m_Track.EvaluateTangent(t)));
}
}