Add missing null check and fallback for DebugRaycaster.cs

This commit is contained in:
Ben C 2022-06-19 13:38:19 -04:00
parent 979c09b1b5
commit 5088749539

View File

@ -1,3 +1,4 @@
using NewHorizons.Components.Orbital;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
@ -15,8 +16,8 @@ namespace NewHorizons.Utility.DebugUtilities
private GameObject _planeUpLeftSphere; private GameObject _planeUpLeftSphere;
private GameObject _planeDownRightSphere; private GameObject _planeDownRightSphere;
private GameObject _planeDownLeftSphere; private GameObject _planeDownLeftSphere;
private void Awake() private void Awake()
{ {
_rb = this.GetRequiredComponent<OWRigidbody>(); _rb = this.GetRequiredComponent<OWRigidbody>();
@ -40,36 +41,42 @@ namespace NewHorizons.Utility.DebugUtilities
{ {
DebugRaycastData data = Raycast(); DebugRaycastData data = Raycast();
if (!data.hit)
{
Logger.Log("Debug Raycast Didn't Hit Anything! (Try moving closer)");
return;
}
var posText = $"{{\"x\": {data.pos.x}, \"y\": {data.pos.y}, \"z\": {data.pos.z}}}"; var posText = $"{{\"x\": {data.pos.x}, \"y\": {data.pos.y}, \"z\": {data.pos.z}}}";
var normText = $"{{\"x\": {data.norm.x}, \"y\": {data.norm.y}, \"z\": {data.norm.z}}}"; var normText = $"{{\"x\": {data.norm.x}, \"y\": {data.norm.y}, \"z\": {data.norm.z}}}";
if(_surfaceSphere != null) GameObject.Destroy(_surfaceSphere); if(_surfaceSphere != null) GameObject.Destroy(_surfaceSphere);
if(_normalSphere1 != null) GameObject.Destroy(_normalSphere1); if(_normalSphere1 != null) GameObject.Destroy(_normalSphere1);
if(_normalSphere2 != null) GameObject.Destroy(_normalSphere2); if(_normalSphere2 != null) GameObject.Destroy(_normalSphere2);
if(_planeUpRightSphere != null) GameObject.Destroy(_planeUpRightSphere ); if(_planeUpRightSphere != null) GameObject.Destroy(_planeUpRightSphere );
if(_planeUpLeftSphere != null) GameObject.Destroy(_planeUpLeftSphere ); if(_planeUpLeftSphere != null) GameObject.Destroy(_planeUpLeftSphere );
if(_planeDownLeftSphere != null) GameObject.Destroy(_planeDownLeftSphere ); if(_planeDownLeftSphere != null) GameObject.Destroy(_planeDownLeftSphere );
if(_planeDownRightSphere != null) GameObject.Destroy(_planeDownRightSphere); if(_planeDownRightSphere != null) GameObject.Destroy(_planeDownRightSphere);
_surfaceSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, 0.1f, Color.green); _surfaceSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, 0.1f, Color.green);
_normalSphere1 = AddDebugShape.AddSphere(data.hitBodyGameObject, 0.01f, Color.red); _normalSphere1 = AddDebugShape.AddSphere(data.hitBodyGameObject, 0.01f, Color.red);
_normalSphere2 = AddDebugShape.AddSphere(data.hitBodyGameObject, 0.01f, Color.red); _normalSphere2 = AddDebugShape.AddSphere(data.hitBodyGameObject, 0.01f, Color.red);
_surfaceSphere.transform.localPosition = data.pos; _surfaceSphere.transform.localPosition = data.pos;
_normalSphere1.transform.localPosition = data.pos + data.norm * 0.5f; _normalSphere1.transform.localPosition = data.pos + data.norm * 0.5f;
_normalSphere2.transform.localPosition = data.pos + data.norm; _normalSphere2.transform.localPosition = data.pos + data.norm;
// plane corners // plane corners
var planeSize = 0.5f; var planeSize = 0.5f;
var planePointSize = 0.05f; var planePointSize = 0.05f;
_planeUpRightSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.green); _planeUpRightSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.green);
_planeUpLeftSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.cyan) ; _planeUpLeftSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.cyan) ;
_planeDownLeftSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.blue) ; _planeDownLeftSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.blue) ;
_planeDownRightSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.cyan) ; _planeDownRightSphere = AddDebugShape.AddSphere(data.hitBodyGameObject, planePointSize, Color.cyan) ;
_planeUpRightSphere .transform.localPosition = data.plane.origin + data.plane.u*1*planeSize + data.plane.v*1*planeSize; _planeUpRightSphere .transform.localPosition = data.plane.origin + data.plane.u*1*planeSize + data.plane.v*1*planeSize;
_planeUpLeftSphere .transform.localPosition = data.plane.origin + data.plane.u*-1*planeSize + data.plane.v*1*planeSize; _planeUpLeftSphere .transform.localPosition = data.plane.origin + data.plane.u*-1*planeSize + data.plane.v*1*planeSize;
_planeDownLeftSphere .transform.localPosition = data.plane.origin + data.plane.u*-1*planeSize + data.plane.v*-1*planeSize; _planeDownLeftSphere .transform.localPosition = data.plane.origin + data.plane.u*-1*planeSize + data.plane.v*-1*planeSize;
_planeDownRightSphere.transform.localPosition = data.plane.origin + data.plane.u*1*planeSize + data.plane.v*-1*planeSize; _planeDownRightSphere.transform.localPosition = data.plane.origin + data.plane.u*1*planeSize + data.plane.v*-1*planeSize;
Logger.Log($"Raycast hit \"position\": {posText}, \"normal\": {normText} on [{data.bodyName}] at [{data.bodyPath}]"); Logger.Log($"Raycast hit \"position\": {posText}, \"normal\": {normText} on [{data.bodyName}] at [{data.bodyPath}]");
@ -91,12 +98,12 @@ namespace NewHorizons.Utility.DebugUtilities
var o = hitInfo.transform.gameObject; var o = hitInfo.transform.gameObject;
var hitAstroObject = o.GetComponent<AstroObject>() ?? o.GetComponentInParent<AstroObject>(); var hitAstroObject = o.GetComponent<AstroObject>() ?? o.GetComponentInParent<AstroObject>();
data.bodyName = o.name; data.bodyName = o.name;
data.bodyPath = SearchUtilities.GetPath(o.transform); data.bodyPath = SearchUtilities.GetPath(o.transform);
data.hitObject = o; data.hitObject = o;
data.hitBodyGameObject = hitAstroObject?.gameObject; data.hitBodyGameObject = hitAstroObject?.gameObject ?? o;
data.plane = ConstructPlane(data); data.plane = ConstructPlane(data);
} }
_rb.EnableCollisionDetection(); _rb.EnableCollisionDetection();
@ -132,6 +139,6 @@ namespace NewHorizons.Utility.DebugUtilities
v = v v = v
}; };
return p; return p;
} }
} }
} }