From 136a50305827ace6e6bb3c0b25fc38ba34f7c8b2 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 9 Nov 2022 13:06:49 -0800 Subject: [PATCH] make raycast nicer --- .../Utility/DebugUtilities/DebugRaycaster.cs | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/NewHorizons/Utility/DebugUtilities/DebugRaycaster.cs b/NewHorizons/Utility/DebugUtilities/DebugRaycaster.cs index 70ecf7f2..5bae97d4 100644 --- a/NewHorizons/Utility/DebugUtilities/DebugRaycaster.cs +++ b/NewHorizons/Utility/DebugUtilities/DebugRaycaster.cs @@ -113,32 +113,36 @@ namespace NewHorizons.Utility.DebugUtilities } internal DebugRaycastData Raycast() { - DebugRaycastData data = new DebugRaycastData(); + var data = new DebugRaycastData(); _rb.DisableCollisionDetection(); - int layerMask = OWLayerMask.physicalMask; - var origin = Locator.GetActiveCamera().transform.position; - var direction = Locator.GetActiveCamera().transform.TransformDirection(Vector3.forward); - - data.hit = Physics.Raycast(origin, direction, out RaycastHit hitInfo, 100f, layerMask); - if (data.hit) + try { - data.pos = hitInfo.transform.InverseTransformPoint(hitInfo.point); - data.norm = hitInfo.transform.InverseTransformDirection(hitInfo.normal); + int layerMask = OWLayerMask.physicalMask; + var origin = Locator.GetActiveCamera().transform.position; + var direction = Locator.GetActiveCamera().transform.TransformDirection(Vector3.forward); - var o = hitInfo.transform.gameObject; + data.hit = Physics.Raycast(origin, direction, out var hitInfo, 100f, layerMask); + if (data.hit) + { + data.pos = hitInfo.rigidbody.transform.InverseTransformPoint(hitInfo.point); + data.norm = hitInfo.rigidbody.transform.InverseTransformDirection(hitInfo.normal); - var hitAstroObject = o.GetComponent() ?? o.GetComponentInParent(); - - data.colliderPath = hitInfo.collider.transform.GetPath(); - data.bodyPath = hitInfo.rigidbody?.transform.GetPath(); - data.hitObject = o; - data.hitBodyGameObject = hitAstroObject?.gameObject ?? o; - data.plane = ConstructPlane(data); - - var toPlayer = Vector3.ProjectOnPlane((transform.position - hitInfo.point).normalized, hitInfo.normal); - var worldSpaceRot = Quaternion.LookRotation(toPlayer, hitInfo.normal); - data.rot = hitAstroObject.transform.InverseTransformRotation(worldSpaceRot); + var toPlayer = Vector3.ProjectOnPlane((transform.position - hitInfo.point).normalized, hitInfo.normal); + var worldSpaceRot = Quaternion.LookRotation(toPlayer, hitInfo.normal); + data.rot = hitInfo.rigidbody.transform.InverseTransformRotation(worldSpaceRot); + + data.colliderPath = hitInfo.collider.transform.GetPath(); + data.bodyPath = hitInfo.rigidbody.transform.GetPath(); + data.hitBodyGameObject = hitInfo.rigidbody.gameObject; + data.hitObject = hitInfo.collider.gameObject; + + data.plane = ConstructPlane(data); + } + } + catch + { + // ignored } _rb.EnableCollisionDetection();