53 lines
1.8 KiB
C#
53 lines
1.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Scripting.APIUpdating;
|
|
|
|
namespace Streamingle.Utils.LerpConstraints
|
|
{
|
|
[MovedFrom(true, "Streamingle.Utils.RoughConstraints", "Assembly-CSharp", "RoughAimConstraint")]
|
|
[ExecuteAlways]
|
|
[AddComponentMenu("Streamingle Utilities/Constraints/Lerp Aim Constraint")]
|
|
public class LerpAimConstraint : LerpConstraintBase
|
|
{
|
|
[Header("Aim")]
|
|
public Vector3 aimAxis = Vector3.forward;
|
|
public Vector3 upAxis = Vector3.up;
|
|
public Vector3 worldUpVector = Vector3.up;
|
|
public Vector3 rotationOffset;
|
|
|
|
protected override void ExecuteConstraint(float followT)
|
|
{
|
|
if (!TryGetWeightedPosition(out var targetPosition))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var direction = targetPosition - transform.position;
|
|
if (direction.sqrMagnitude < 0.000001f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var targetRotation = LookRotationForAxis(direction, worldUpVector, aimAxis, upAxis);
|
|
targetRotation *= Quaternion.Euler(rotationOffset);
|
|
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, followT * weight);
|
|
}
|
|
|
|
protected override void CaptureOffsetsFromCurrentPose()
|
|
{
|
|
if (!TryGetWeightedPosition(out var targetPosition))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var direction = targetPosition - transform.position;
|
|
if (direction.sqrMagnitude < 0.000001f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var baseRotation = LookRotationForAxis(direction, worldUpVector, aimAxis, upAxis);
|
|
rotationOffset = (Quaternion.Inverse(baseRotation) * transform.rotation).eulerAngles;
|
|
}
|
|
}
|
|
}
|