Fix debug raycast normal

This commit is contained in:
Nick 2022-05-04 17:36:57 -04:00
parent 1545c3eb1c
commit 63d72fa723
2 changed files with 20 additions and 5 deletions

View File

@ -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<SphereCollider>().enabled = false;
@ -14,7 +14,7 @@ namespace NewHorizons.Utility
sphere.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Sprites/Default"));
sphere.GetComponent<MeshRenderer>().material.color = color;
sphere.AddComponent<MakeMeshDoubleFaced>();
return sphere.gameObject;
}
}
}

View File

@ -14,6 +14,9 @@ namespace NewHorizons.Utility
public class DebugRaycaster : MonoBehaviour
{
private OWRigidbody _rb;
private GameObject _surfaceSphere;
private GameObject _normalSphere1;
private GameObject _normalSphere2;
private void Awake()
{
@ -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();
}