Raycast2D Detecting Something That It Shouldn't

I’ve been trying to learn how to use Raycast2D and just when I thought I had it figured out, an issue arose:

Raycast is detecting an object that isn’t there.

My test code looks like this:

function Update ()
{
     // Cast a ray to detect if there is ground below the object
     var hit : RaycastHit2D = Physics2D.Raycast (transform.position, -Vector2.up);

     print ("The collider hit was: " + hit.collider);

     // If the ray we just casted hits the seafloor indicating ground is below the object...
     if (hit.collider != null)
     {
          // Apply gravity to have the object align itself with the slope of the seafloor
          GetComponent (Rigidbody2D).gravityScale = 1;
     }
}

The problem is that hit.collider returns an object that it didn’t hit for the first 2 frames after level load. Then from the 3rd frame onward, it returns null.

And no, the objects involved are not moving. The object doing the raycasting spawns in an area where nothing is below it.

Any help would be greatly appreciated!

– Chris

Yup, still can’t figure this out, but I’m still trying.

UPDATE: Well it seems to have fixed itself. Changed nothing and now I can’t get it to happen again. Weird o.o But I’m sure it’ll still crop up later on, so still need a fix for it.

What object was being hit? Was it the same object every time it happened? By ‘not there’ do you mean it shouldn’t have been intersected by the cast, or that it wasn’t in the scene?

If it happened to be an object that will never be checked by this raycast, you can use a LayerMask to prevent it from being hit. I can’t imagine why that would happen if there are no moving or repositioned objects at all.

You mentioned a level load-- are you switching between scenes when this happens, or is this just one scene loaded into the Editor, then you push the Play button to see the issue?

Have you updated Unity since you started seeing this issue? I’m wondering whether it could have been a bug that was fixed or something.

It was always the same object every time even though it wasn’t beneath the object. And by ‘not there’ I mean that the object isn’t beneath the object doing the raycasting. Specifically, I have 3 seafloor segments. I have the ship doing the raycasting to ensure that seafloor is below it for it to land on. But sometimes, when the ship spawns and does the raycasting, it detects the 2nd seafloor segment (which is the middle segment). I have no idea why it is doing this.

I cannot ignore any of the seafloor segments, as I need to see if they’re below the ship so that it can apply gravity to have it drop and land on the seafloor.

And I am not switching between scenes when this happens. I push the play button to see the issue (the ship first spawns in the Awake function).

I am using version 5.6.1f1.

EDIT: I disabled the 2nd seafloor segment to test to see if its removal would fix the problem and it has (pretty sure). Out of 100 tests, the ship raycasted properly. Now am going to test with a different segment in its place. I’ll update this post with the result when done.

EDIT: With the 3rd seafloor segment in place of the 2nd one, as far as I can tell, it works fine. Out of many tests (40+) it raycasted properly.

I am led to believe the problem is with the 2nd seafloor segment. But that makes no sense because it works just the same as the other 2 seafloor segments. So I am really confused. :face_with_spiral_eyes:

Not sure if it relates to your problem or not but there was a bug introduced with Raycast vs EdgeCollider2D. I have a fix that is being processed for 5.5, 5.6, 2017.1 & 2017.2 right now.

Thanks, but I’m not using EdgeCollider2D. All colliders are PolygonCollider2Ds.

If you have a simple reproduction case then a bug report would help find any issue. Alternately just providing a link to a simple reproduction project.

Apologies for the delay.

The problem is it’s rare that it happens. So unless someone’s going to test it 100 times, nothing will probably be replicated. But I can try to make a reproduction.