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

33 lines
1.1 KiB
C#
Raw Normal View History

2025-01-07 02:06:59 +01:00
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Splines;
using Interpolators = UnityEngine.Splines.Interpolators;
namespace Unity.Splines.Examples
{
public class AnimateLookAt : MonoBehaviour
{
[SerializeField]
SplineAnimate m_SplineAnimate;
[SerializeField]
PointSplineData m_LookAtPoints;
[SerializeField]
Transform m_LookTransform;
void LateUpdate()
{
if(m_SplineAnimate == null || m_LookAtPoints == null || m_LookTransform == null)
return;
var spline = m_SplineAnimate.splineContainer.Spline;
var t = m_SplineAnimate.normalizedTime;
var lookAtPoint = m_LookAtPoints.points.Evaluate(spline, t, PathIndexUnit.Normalized, new Interpolators.SlerpFloat2());
var direction = math.normalize(new float3(lookAtPoint.x, 0f, lookAtPoint.y) - (float3)m_LookTransform.position);
m_LookTransform.transform.rotation = Quaternion.LookRotation(direction, Vector3.up);
}
}
}