From 4d8fd9213fee9934e5b439f81c340b418ff0da72 Mon Sep 17 00:00:00 2001 From: Josh Wittner Date: Wed, 27 Jul 2016 21:42:11 -0700 Subject: [PATCH] Fixed Billboards alignment across all axes. --- .../Utilities/Scripts/Billboard.cs | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/Assets/HoloToolkit/Utilities/Scripts/Billboard.cs b/Assets/HoloToolkit/Utilities/Scripts/Billboard.cs index e6bca5bdcab..ab9baccd79c 100644 --- a/Assets/HoloToolkit/Utilities/Scripts/Billboard.cs +++ b/Assets/HoloToolkit/Utilities/Scripts/Billboard.cs @@ -43,33 +43,32 @@ private void Awake() private void Update() { // Get a Vector that points from the Camera to the target. - Vector3 directionToTarget = Camera.main.transform.position - gameObject.transform.position; - - // If we are right next to the camera the rotation is undefined. - if (directionToTarget.sqrMagnitude < Mathf.Epsilon) - { - return; - } + Vector3 forward; + Vector3 up; // Adjust for the pivot axis. switch (PivotAxis) { case PivotAxis.X: - directionToTarget.x = gameObject.transform.position.x; + forward = Vector3.ProjectOnPlane(Camera.main.transform.forward, Vector3.right).normalized; + up = Vector3.Cross(forward, Vector3.right); break; case PivotAxis.Y: - directionToTarget.y = gameObject.transform.position.y; + up = Vector3.up; + forward = Vector3.ProjectOnPlane(Camera.main.transform.forward, up); break; case PivotAxis.Free: default: - // No changes needed. + forward = Camera.main.transform.forward; + up = Camera.main.transform.up; break; } - // Calculate and apply the rotation required to reorient the object and apply the default rotation to the result. - gameObject.transform.rotation = Quaternion.LookRotation(-directionToTarget) * DefaultRotation; + + // Calculate and apply the rotation required to reorient the object + gameObject.transform.rotation = Quaternion.LookRotation(forward, up); } } } \ No newline at end of file