- Home /
CapsuleCastNonAlloc not filling results buffer in 2018.2
Running into some strange behavior. Due to an issue with the capsule cast I am testing, I wrote this simple test script:
public class TestCapsuleCast : MonoBehaviour {
Material m_mat;
CapsuleCollider m_capsule;
int collidableLayers;
RaycastHit[] hitResults;
RaycastHit[] hitResults2;
private void Start()
{
m_mat = GetComponentInChildren<Renderer>().material;
m_capsule = GetComponent<CapsuleCollider>();
collidableLayers = 0;
for (int i = 0; i < 32; i++)
{
if (!Physics.GetIgnoreLayerCollision(this.gameObject.layer, i))
{
collidableLayers |= (1 << i);
}
}
hitResults = new RaycastHit[4];
hitResults2 = new RaycastHit[4];
}
private void Update()
{
int numHits = Physics.CapsuleCastNonAlloc(
transform.position + Vector3.down * (m_capsule.height / 2f - m_capsule.radius) + Vector3.up,
transform.position + Vector3.up * (m_capsule.height / 2f - m_capsule.radius) + Vector3.up,
m_capsule.radius,
Vector3.down,
hitResults,
1f,
collidableLayers,
QueryTriggerInteraction.Ignore);
hitResults2 = Physics.CapsuleCastAll(
transform.position + Vector3.down * (m_capsule.height / 2f - m_capsule.radius) + Vector3.up,
transform.position + Vector3.up * (m_capsule.height / 2f - m_capsule.radius) + Vector3.up,
m_capsule.radius,
Vector3.down,
1f,
collidableLayers,
QueryTriggerInteraction.Ignore);
if (numHits > 1)
{
m_mat.color = Color.green;
}
else
{
m_mat.color = Color.red;
}
for (int i=0; i<4; i++)
{
Collider col = hitResults[i].collider;
if (col != null)
Debug.Log(col);
}
foreach (RaycastHit hit in hitResults2)
{
Collider col = hit.collider;
if (col != null)
Debug.Log(col);
}
}
}
The behavior I'm seeing is that the CapsuleCastNonAlloc is returning the correct number of hits, but all of the values in the hitResults buffer it was meant to fill have a null collider, 0 distance, etc. like the base values for the RaycastHit, but CapsuleCastAll is correctly returning the hits. Is this a bug or am I setting things up incorrectly?
How exactly do you debug this? Your Debug.Logs are pretty pointless. Use a meaningful log text that can be distinguished. Also you shouldn't iterate through the whole array. Only up to "numHits".
Alternated commenting out the separate debug blocks to see that the results buffer filled by CapsuleCastAll returns non-null colliders while CapsuleCastNonAlloc has all null colliders, or used those spaces as breakpoints to inspect the hits arrays directly to find the above information. You'd usually iterate just through numHits, but in this case since it's not filling the buffer I wanted to be sure it wasn't strangely putting the results somewhere else in the array.