Skip to content

Commit

Permalink
Merge pull request #30 from ehsan-mohammadi/dev
Browse files Browse the repository at this point in the history
Change LookAt2D to extension method
  • Loading branch information
ehsan-mohammadi authored Sep 30, 2019
2 parents 5d680f1 + 3b0ff3c commit 74fb0aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
20 changes: 3 additions & 17 deletions Aim-IK/AimIKBehaviour2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,12 @@ private void CheckClamp(Transform part, AxisLimitRotation limitRotation, float r
rotation = AimIKFunctions.ClampAngle(part.localEulerAngles.z, limitRotation.min, limitRotation.max);
else
rotation = part.localEulerAngles.z;

// Set rotation variables to part rotation
Vector3 partRotation = new Vector3(part.localEulerAngles.x, part.localEulerAngles.y, rotation);
part.localEulerAngles = partRotation;
}

/// <summary>
/// Rotates the 2D transforms so the right vector points at worldPosition
/// </summary>
/// <param name="transform">The transform that should to look at</param>
/// <param name="worldPosition">Point to look at</param>
private void LookAt2D(Transform transform, Vector2 worldPosition)
{
Vector2 diff = worldPosition - new Vector2(transform.position.x, transform.position.y);
diff.Normalize();

float rotateZ = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotateZ);
}

/// <summary>
/// LateUpdate called after Update and FixedUpdate functions each frames. This function is on top of any animation.
/// </summary>
Expand All @@ -60,7 +46,7 @@ private void LateUpdate()
{
if (chestPart.part && target) // If chest part and target exists
{
LookAt2D(chestPart.part, new Vector2(target.position.x, target.position.y) - eyesOffset);
chestPart.part.LookAt2D(new Vector2(target.position.x, target.position.y) - eyesOffset);
CheckClamp(chestPart.part, chestPart.limitRotation, chestPart.GetRotation());
}
}
Expand All @@ -69,7 +55,7 @@ private void LateUpdate()
// If head and target exists
if (head && target)
{
LookAt2D(head, new Vector2(target.position.x, target.position.y) - eyesOffset);
head.LookAt2D(new Vector2(target.position.x, target.position.y) - eyesOffset);
CheckClamp(head, headLimitRotation, headRotation);
}
}
Expand Down
16 changes: 15 additions & 1 deletion Aim-IK/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace AimIK.Functions
{
public class AimIKFunctions
public static class AimIKFunctions
{
/// <summary>
/// Clamp the angle between min and max
Expand Down Expand Up @@ -57,5 +57,19 @@ private static float NormalizeAngle(float angle)

return angle;
}

/// <summary>
/// Rotates the 2D transforms so the right vector points at worldPosition
/// </summary>
/// <param name="transform">The transform that should to look at</param>
/// <param name="worldPosition">Point to look at</param>
public static void LookAt2D(this Transform transform, Vector2 worldPosition)
{
Vector2 diff = worldPosition - new Vector2(transform.position.x, transform.position.y);
diff.Normalize();

float rotateZ = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotateZ);
}
}
}

0 comments on commit 74fb0aa

Please sign in to comment.