make raycast nicer

This commit is contained in:
JohnCorby 2022-11-09 13:06:49 -08:00
parent 967d909651
commit 136a503058

View File

@ -113,32 +113,36 @@ namespace NewHorizons.Utility.DebugUtilities
} }
internal DebugRaycastData Raycast() internal DebugRaycastData Raycast()
{ {
DebugRaycastData data = new DebugRaycastData(); var data = new DebugRaycastData();
_rb.DisableCollisionDetection(); _rb.DisableCollisionDetection();
int layerMask = OWLayerMask.physicalMask; try
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)
{ {
data.pos = hitInfo.transform.InverseTransformPoint(hitInfo.point); int layerMask = OWLayerMask.physicalMask;
data.norm = hitInfo.transform.InverseTransformDirection(hitInfo.normal); 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<AstroObject>() ?? o.GetComponentInParent<AstroObject>(); 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.colliderPath = hitInfo.collider.transform.GetPath();
data.bodyPath = hitInfo.rigidbody?.transform.GetPath(); data.bodyPath = hitInfo.rigidbody.transform.GetPath();
data.hitObject = o; data.hitBodyGameObject = hitInfo.rigidbody.gameObject;
data.hitBodyGameObject = hitAstroObject?.gameObject ?? o; data.hitObject = hitInfo.collider.gameObject;
data.plane = ConstructPlane(data);
var toPlayer = Vector3.ProjectOnPlane((transform.position - hitInfo.point).normalized, hitInfo.normal); data.plane = ConstructPlane(data);
var worldSpaceRot = Quaternion.LookRotation(toPlayer, hitInfo.normal); }
data.rot = hitAstroObject.transform.InverseTransformRotation(worldSpaceRot); }
catch
{
// ignored
} }
_rb.EnableCollisionDetection(); _rb.EnableCollisionDetection();