From 63d72fa723d37a928a447387edde3746d66b0826 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 4 May 2022 17:36:57 -0400 Subject: [PATCH] Fix debug raycast normal --- NewHorizons/Utility/AddDebugShape.cs | 4 ++-- NewHorizons/Utility/DebugRaycaster.cs | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/NewHorizons/Utility/AddDebugShape.cs b/NewHorizons/Utility/AddDebugShape.cs index 48bb32e4..fb934286 100644 --- a/NewHorizons/Utility/AddDebugShape.cs +++ b/NewHorizons/Utility/AddDebugShape.cs @@ -4,7 +4,7 @@ namespace NewHorizons.Utility { static class AddDebugShape { - public static void AddSphere(GameObject obj, float radius, Color color) + public static GameObject AddSphere(GameObject obj, float radius, Color color) { var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.GetComponent().enabled = false; @@ -14,7 +14,7 @@ namespace NewHorizons.Utility sphere.GetComponent().material = new Material(Shader.Find("Sprites/Default")); sphere.GetComponent().material.color = color; - sphere.AddComponent(); + return sphere.gameObject; } } } diff --git a/NewHorizons/Utility/DebugRaycaster.cs b/NewHorizons/Utility/DebugRaycaster.cs index e8c1b4b3..8060f2ec 100644 --- a/NewHorizons/Utility/DebugRaycaster.cs +++ b/NewHorizons/Utility/DebugRaycaster.cs @@ -14,7 +14,10 @@ namespace NewHorizons.Utility public class DebugRaycaster : MonoBehaviour { private OWRigidbody _rb; - + private GameObject _surfaceSphere; + private GameObject _normalSphere1; + private GameObject _normalSphere2; + private void Awake() { _rb = this.GetRequiredComponent(); @@ -32,13 +35,25 @@ namespace NewHorizons.Utility if (Physics.Raycast(origin, direction, out RaycastHit hitInfo, 100f, layerMask)) { var pos = hitInfo.transform.InverseTransformPoint(hitInfo.point); - var norm = hitInfo.normal; + var norm = hitInfo.transform.InverseTransformDirection(hitInfo.normal); var o = hitInfo.transform.gameObject; var posText = $"{{\"x\": {pos.x}, \"y\": {pos.y}, \"z\": {pos.z}}}"; var normText = $"{{\"x\": {norm.x}, \"y\": {norm.y}, \"z\": {norm.z}}}"; - Logger.Log($"Raycast hit pos: {posText}, normal: {normText} on [{o.name}] at [{SearchUtilities.GetPath(o.transform)}]"); + if(_surfaceSphere != null) GameObject.Destroy(_surfaceSphere); + if(_normalSphere1 != null) GameObject.Destroy(_normalSphere1); + if(_normalSphere2 != null) GameObject.Destroy(_normalSphere2); + + _surfaceSphere = AddDebugShape.AddSphere(hitInfo.transform.gameObject, 0.1f, Color.green); + _normalSphere1 = AddDebugShape.AddSphere(hitInfo.transform.gameObject, 0.01f, Color.red); + _normalSphere2 = AddDebugShape.AddSphere(hitInfo.transform.gameObject, 0.01f, Color.red); + + _surfaceSphere.transform.localPosition = pos; + _normalSphere1.transform.localPosition = pos + norm * 0.5f; + _normalSphere2.transform.localPosition = pos + norm; + + Logger.Log($"Raycast hit \"position\": {posText}, \"normal\": {normText} on [{o.name}] at [{SearchUtilities.GetPath(o.transform)}]"); } _rb.EnableCollisionDetection(); }