51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using UnityEngine;
|
|
|
|
namespace Streamingle.Utils.RoughConstraints
|
|
{
|
|
[ExecuteAlways]
|
|
[AddComponentMenu("Streamingle Utilities/Constraints/Rough Aim Constraint")]
|
|
public class RoughAimConstraint : RoughConstraintBase
|
|
{
|
|
[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;
|
|
}
|
|
}
|
|
}
|