From 063735b0e6a74dd6d4b97e0d5da81004855ba35e Mon Sep 17 00:00:00 2001 From: misabiko Date: Sun, 14 Jun 2020 19:00:27 -0400 Subject: [PATCH] Calculated angle from -180:180 to 0:360 --- Assets/Scripts/PlayerMovement.cs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs index 3bd640a..9008ec5 100644 --- a/Assets/Scripts/PlayerMovement.cs +++ b/Assets/Scripts/PlayerMovement.cs @@ -44,8 +44,7 @@ public class PlayerMovement : MonoBehaviour { //free moveDirection Vector3 direction = camForward * moveInput.y + camTransform.right * moveInput.x; - float angle = Vector3.SignedAngle(Vector3.forward, direction, Vector3.up); - freeAngleText.text = "FreeAngle: " + Mathf.RoundToInt(angle); + freeAngleText.text = "FreeAngle: " + Mathf.RoundToInt(GetAngle(direction)); lastDirection = GetDirection(direction); directionText.text = "Direction: " + lastDirection; @@ -61,23 +60,25 @@ public class PlayerMovement : MonoBehaviour { moveDirection = direction; } - float GetRoundAngleOffset(Vector3 direction) { - float angle = Vector3.SignedAngle(Vector3.forward, moveDirection, Vector3.up); + float GetAngle(Vector3 direction) { + float angle = Vector3.SignedAngle(Vector3.forward, direction, Vector3.up); + if (angle < 0) + angle += 360f; - float deltaAngle = Mathf.Sign(angle) * 29; - deltaAngle -= (deltaAngle + angle) % 60; - - return deltaAngle; + return angle; } + float GetRoundAngleOffset(Vector3 direction) + => 29 - (GetAngle(direction) + 29) % 60; + Vector3 RoundedDirection(Vector3 direction) => Quaternion.AngleAxis(GetRoundAngleOffset(direction), Vector3.up) * direction; MapData.Direction GetDirection(Vector3 direction) { - float angle = Vector3.SignedAngle(Vector3.forward, direction, Vector3.up); + float angle = GetAngle(direction); float deltaAngle = GetRoundAngleOffset(direction); - int index = (Mathf.RoundToInt(angle + deltaAngle) / 60 + (Mathf.Sign(angle) < 0 ? 6 : 0)) % 6; + int index = (Mathf.RoundToInt(angle + deltaAngle) / 60) % 6; return (MapData.Direction) index; }