I’m trying to visualize several HitCast methods in Debug mode, I’m having a issue with Physics.Hitcast: the resulting RayCastHit.point sometimes is right, some other times is just the origin of the Gameobject shooting the Boxcast.
I looked up the problem and
- My Boxcast is not starting inside the object (as you can see in the picture)
- The gameobject from where the Boxcast starts, has no colliders anyway
What is even more curious is that even if the hit.point is wrong, hit.distance is correct…
Here is my code
private Transform tr;
private LayerMask collisionLayers = 255;
private bool[] hitDetected = new bool[128];
private RaycastHit currentHit;
RaycastHit hit;
private void Start() {
tr = GetComponent<Transform>();
collisionLayers = 255;
}
private void Update() {
hitDetected[0] = Physics.BoxCast(tr.position, Vector3.up * rayscanBoxWidth / 2 + Vector3.right * rayscanBoxWidth / 2, tr.forward, out hit, transform.rotation, rayscanLength, collisionLayers, QueryTriggerInteraction.Ignore);
if (hitDetected[0]) {
Debug.Log(hit.point + " " + hit.distance + " " + hit.transform.name);
}
}
private void OnDrawGizmos() {
Gizmos.color = hitDetected[0] ? Color.red : Color.green;
Gizmos.DrawRay(tr.position, tr.forward * (rayscanLength));
if (hitDetected[0]) {
Gizmos.DrawCube(hit.point, Vector3.one * rayscanBoxWidth);
}
}