Skip to content

Commit

Permalink
feat(Tracking): apply distance offset on position difference rotation
Browse files Browse the repository at this point in the history
The TransformPositionDifferenceRotation logic now has a source to
offset maximum distance setting that will be activated if the
`Apply Offset` is to true. This will ensure the rotation will only
be applied when the source and offset are within the given distances
and anything outside of those distances will be ignored.
  • Loading branch information
thestonefox committed Apr 17, 2024
1 parent a88eca9 commit 208570d
Showing 1 changed file with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
/// </summary>
public class TransformPositionDifferenceRotation : PropertyModifier
{
[Tooltip("The maximum distance the Source GameObject can be from the Offset GameObject to allow the rotation to apply.")]
[SerializeField]
private Vector3 sourceToOffsetMaximumDistance = Vector3.one * float.PositiveInfinity;
/// <summary>
/// The maximum distance the Source <see cref="GameObject"/> can be from the Offset <see cref="GameObject"/> to allow the rotation to apply.
/// </summary>
public Vector3 SourceToOffsetMaximumDistance
{
get
{
return sourceToOffsetMaximumDistance;
}
set
{
sourceToOffsetMaximumDistance = value;
}
}
[Tooltip("The drag applied to the rotation to slow it down.")]
[SerializeField]
private float angularDrag = 1f;
Expand Down Expand Up @@ -127,7 +144,7 @@ public virtual void ResetPreviousState()
/// <param name="offset">The offset of the _target against the source when modifying.</param>
protected override void DoModify(GameObject source, GameObject _target, GameObject offset = null)
{
AngularVelocity = CalculateAngularVelocity(source, _target);
AngularVelocity = CalculateAngularVelocity(source, _target, offset);
_target.transform.localRotation *= Quaternion.Euler(AngularVelocity);
}

Expand All @@ -141,18 +158,25 @@ protected virtual void OnDisable()
/// </summary>
/// <param name="source">The source to utilize in the modification.</param>
/// <param name="_target">The _target to modify.</param>
/// <param name="offset">The offset of the _target against the source when modifying.</param>
/// <returns>The angular velocity to project onto the _target.</returns>
protected virtual Vector3 CalculateAngularVelocity(GameObject source, GameObject _target)
protected virtual Vector3 CalculateAngularVelocity(GameObject source, GameObject _target, GameObject offset)
{
Vector3 negatePosition = Ancestor != null ? Ancestor.transform.position : Vector3.zero;
Vector3 sourcePosition = source.transform.position - negatePosition;
Vector3 _targetPosition = _target.transform.position - negatePosition;
Vector3 offsetPosition = offset != null ? offset.transform.position - negatePosition : Vector3.zero;

if (previousSourcePosition == null)
{
previousSourcePosition = sourcePosition;
}

if (offset != null && !sourcePosition.WithinDistance(offsetPosition, SourceToOffsetMaximumDistance))
{
return Vector3.zero;
}

float xDegree = FollowOnAxis.xState ? CalculateAngle(_target.transform.right, _targetPosition, (Vector3)previousSourcePosition, sourcePosition) : 0f;
float yDegree = FollowOnAxis.yState ? CalculateAngle(_target.transform.up, _targetPosition, (Vector3)previousSourcePosition, sourcePosition) : 0f;
float zDegree = FollowOnAxis.zState ? CalculateAngle(_target.transform.forward, _targetPosition, (Vector3)previousSourcePosition, sourcePosition) : 0f;
Expand Down

0 comments on commit 208570d

Please sign in to comment.
  NODES
COMMUNITY 1
Note 1
Project 2
USERS 1