streamingle-unity-utilities/Runtime/Constraints/LerpRotationConstraint.cs

39 lines
1.3 KiB
C#

using UnityEngine;
using UnityEngine.Scripting.APIUpdating;
namespace Streamingle.Utils.LerpConstraints
{
[MovedFrom(true, "Streamingle.Utils.RoughConstraints", "Assembly-CSharp", "RoughRotationConstraint")]
[ExecuteAlways]
[AddComponentMenu("Streamingle Utilities/Constraints/Lerp Rotation Constraint")]
public class LerpRotationConstraint : LerpConstraintBase
{
[Header("Rotation")]
public Vector3Bool constrainedAxis = Vector3Bool.All;
public Vector3 rotationOffset;
protected override void ExecuteConstraint(float followT)
{
if (!TryGetWeightedRotation(out var targetRotation))
{
return;
}
targetRotation *= Quaternion.Euler(rotationOffset);
var maskedEuler = ApplyEulerAxisMask(transform.rotation, targetRotation, constrainedAxis);
targetRotation = Quaternion.Euler(maskedEuler);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, followT * weight);
}
protected override void CaptureOffsetsFromCurrentPose()
{
if (!TryGetWeightedRotation(out var sourceRotation))
{
return;
}
rotationOffset = (Quaternion.Inverse(sourceRotation) * transform.rotation).eulerAngles;
}
}
}