0
\$\begingroup\$

I'm trying to debug the following situation:

enter image description here

The raycast returns miss even though it clearly hits its target collider.

The target is a stock 3D Object Unity Cube. The script firing the rays has a Public GameObject called 'target', the cube is correctly set as the target.

Here is the bulk of the raycasting I am doing. The rays are aimed at an array of positions within the bounds of the box called 'vertices'.

This was working yesterday and I didn't change the code and now it no longer works and I am completely stumped with no idea how to even begin to debug why.

    for (int i = 0; i < vertices.Length; i++)
    {
        Vector3 direction = vertices[i] - self.position;

        Ray ray = new Ray(self.position, direction);

        if (Physics.Raycast(ray, out raycastHit))
        {
            if (raycastHit.collider.transform == target)
            {
                Debug.DrawRay(self.position, direction, Color.green, 5.0f, true);
                hitCount += 1f;
            }
            else
            {
                Debug.DrawRay(self.position, direction, Color.red, 5.0f, true);
            }
        }
    }
\$\endgroup\$
5
  • \$\begingroup\$ Can you show us how you populate your vertices array? \$\endgroup\$
    – DMGregory
    Nov 19 '20 at 3:31
  • \$\begingroup\$ When you say "no longer works" what exactly does it do? Does it output nothing? Does it give the wrong output? \$\endgroup\$
    – Theraot
    Nov 19 '20 at 8:09
  • \$\begingroup\$ Also, in your screenshot, I can't help but notice that we see the green outline of a box collider behind your cube, but no collider outline around your cube itself. Is that just a coincidence, or have you modified the collider's position relative to the display mesh? (If the screenshot weren't cropped so tight and we could see the inspector, we might not need to ask that) \$\endgroup\$
    – DMGregory
    Nov 19 '20 at 12:52
  • \$\begingroup\$ The green box is something else entirely, you can ignore it. It no longer works as in all rays return no hit. The vertex array is a matrix and its like a full page of code long. The bug was in if (raycastHit.collider.transform == target). Target is a collider but it was comparing transform to collider for some reason. \$\endgroup\$
    – Bunp
    Nov 19 '20 at 15:10
  • 1
    \$\begingroup\$ @Bunp It was comparing Transform to Collider because that's how your code is written. raycastHit.collider.transform == target. If you don't want to use the transform, change it to raycastHit.collider == target. \$\endgroup\$
    – Kevin
    Nov 19 '20 at 21:37
1
\$\begingroup\$

I am completely stumped with no idea how to even begin to debug why.

Always start with some basic debug logging:

if (Physics.Raycast(ray, out raycastHit))
{
    Debug.Log("Hit collider: " + hit.collider +", target: " + target);
    Debug.Log("hit.collider == target" + hit.collider == target);

The script firing the rays has a Public GameObject called 'target'

Your code says if (raycastHit.collider.transform == target). If target is a GameObject, the result will always be false, because the Transform component is a Component, not a GameObject.

Target is a collider but it was comparing transform to collider for some reason.

Wait, is target a collider or a GameObject? If it's a collider, why are you comparing it to the Transform of the object that the Raycast hit? Collider and Transform are different components and will never be equal.

This will probably do what you want:

if (raycastHit.collider.transform == target.transform)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .