From 3b0ff3c2f8f48145ebfa80208ab20da40fb4f78e Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 30 Sep 2019 18:41:49 +0330 Subject: [PATCH] Change LookAt2D to extension method --- Aim-IK/AimIKBehaviour2D.cs | 20 +++----------------- Aim-IK/Functions.cs | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Aim-IK/AimIKBehaviour2D.cs b/Aim-IK/AimIKBehaviour2D.cs index 6032913..c7c13e5 100644 --- a/Aim-IK/AimIKBehaviour2D.cs +++ b/Aim-IK/AimIKBehaviour2D.cs @@ -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; } - /// - /// Rotates the 2D transforms so the right vector points at worldPosition - /// - /// The transform that should to look at - /// Point to look at - 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); - } - /// /// LateUpdate called after Update and FixedUpdate functions each frames. This function is on top of any animation. /// @@ -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()); } } @@ -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); } } diff --git a/Aim-IK/Functions.cs b/Aim-IK/Functions.cs index 4e871bc..26c0c91 100644 --- a/Aim-IK/Functions.cs +++ b/Aim-IK/Functions.cs @@ -2,7 +2,7 @@ namespace AimIK.Functions { - public class AimIKFunctions + public static class AimIKFunctions { /// /// Clamp the angle between min and max @@ -57,5 +57,19 @@ private static float NormalizeAngle(float angle) return angle; } + + /// + /// Rotates the 2D transforms so the right vector points at worldPosition + /// + /// The transform that should to look at + /// Point to look at + 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); + } } }