Physics2D.Raycast issue

I’m trying to make a randomly generator game map. That has randomly generator ground, water, and floating platforms. This is working great. However on some maps the floating platforms that are generator over the water. Are sometimes to high or to far to reach by jumping. So I decided to check for this and add a jump assist block. They are either a 2 by 2 block, 3 by 2 block or a “L” shape configuration that sort of looks like a bridge when created.

I also decided to add randomly created floating platforms in the sky above the ground. This is where my jump assist blocks become an issue.
Sometimes floating platforms are placed above the jump assist blocks. The player can reach the floating platform above the jump assist block so the jump assist block is not need in those cases.
So I decided to check for this before I add the jump assist block. I thought a Physics2D.Raycast would work. Think of the 2 by 2 square as 2 columns with 2 rows. I am sending up a Raycast in the middle of both columns. If both columns hit a collider (Floating Platform) then the jump assist is not needed. If only 1 column hits a collider then I will still add the jump assist block. If none of the columns hit a collider I will add the jump assist block.

Here is part of the Raycast code I’m using. My floating platforms are part of the Ground Layer. I tried with and without a layer mask.

int layerMask = 1 << LayerMask.NameToLayer(“Ground”);

RaycastHit2D hit = Physics2D.Raycast(origin, Vector2.up, checkDistance, layerMask);
Debug.DrawRay(origin, Vector2.up * checkDistance, Color.red, 60f, false);

RaycastHit2D hit2 = Physics2D.Raycast(origin2, Vector2.up, checkDistance, layerMask);
Debug.DrawRay(origin2, Vector2.up * checkDistance, Color.red, 60f, false);

I’m assuming the Debug.DrawRay is drawing the same path the Physics2D.Raycast is using to hit something. However I’m seeing jump assist blocks with rays that look like there colliding with a platform but I’m getting no hit detected from the Raycast. I’m getting mixed results. 1 block that looks like both rays are hitting a platform returns only 1 hit. Another jump assist block that looks like it’s hitting a floating platform with both columns returns no hits at all.

If anyone can provide me with some insight on what I should try with the Physics2D.Raycast or how to test this better it would be helpful thanks.

Picture of the problem:

It’s hard to tell exactly what your problem might be from the description, but one thing I like to do to have an easier time debugging raycasting is to change the color of the debug line depending on the hit result, and also change the ray length to draw to the hit location if something was hit.

Vector3 end = hit.collider != null ? hit.point : (origin + Vector2.up * checkDistance);
Color color = hit.collider != null ? Color.green : Color.red;
Debug.DrawLine(origin, end, color, 60f, false);

I was hoping the picture I provide would help in visualizing the problem I descripted.

Is your raycast picking up nothing? Or the wrong thing? Are your colliders the right size/position? Are the layers set correctly?

Just verify all your assumptions and you’ll find out why exactly the raycast didn’t hit what you expected.

What’s puzzling is that I get 1 successful hit out of 4. One would think that I should get 4 successful hits but I don’t.

Hopefully you can see from this picture that the Physics2D.Raycast is only returning a Hit on the left side of the platform. The platform is using a Edge Collider 2D to cover the whole platform. Why is the whole platform not being detected by the Physics2D.Raycast is beyond me.

Well considering it’s always the same portion of the platform being hit, I would compare the end-cap tile to the other tiles and see what is different about them.

Can you confirm that those green raycasts are hitting the Edge collider and not something else?

Are you sure the edge collider extends the full length of the platform?

I found another thread in 2015 about a similar issue with EdgeCollider2D found here

EdgeCollider2D and Physics2D.Raycast missing some collisions
http://answers.unity3d.com/questions/1063589/edgecollider2d-and-physics2draycast-missing-some-c.html

If this is a bug I will report it as such. It seems pretty clear to me that the Physics2D.Raycast is not working correctly with the EdgeCollider2D. I’m going to test with other casts to see what happens.

Jeffrey the parent object is the only object that has a EdgeCollider2D attached and it covers the whole outside edge of the platform. The image objects inside the platform have NO colliders attached to them. I’ll post a screen shot of my player standing on the right edge of the platform and a close up of the platform if I have to too prove my point. The Raycast is not working properly with the EdgeCollider2D. I don’t understand why it favors the left side verses the right side. The right side image uses the same object container as the left side image. So they are virtually the same except the image being displayed is different. Again those images do not even have a EdgeCollider2D only their parent does. So it’s still a mystery.

Solved sort of!

This most definitely is a bug that needs to be fixed. I have 2 prefabs setup one for the ground platforms and 1 for the floating platforms. They both have colliders sized to 1 game unit. Which in my case is 1.28f by 1.28f.

I resize the collider depending on the width of the platform. The ground platform has a BoxCollider2D and the floating platform has a EdgeCollider2D. The Physics2D.Raycast and all casts in generator seems to only be honoring the initial size of the collider. This is why the left side only of the collider was only being detected when hit.

Seems strange but I decide to set the max size of the BoxCollider2D and the last X point of the EdgeCollider2D to the max size of my of map to see what would happen. As you can see now I’m getting HITS detected with the RayCast even when it should not be hitting anything. So nearly the opposite of before.

If you never resize your colliders in code. Then try to hit it with a cast then you would most likely never see this problem.

I’m going to try adding the collider through code after I add the platform images to the parent platform to see if that eventually fixes the problem for now.

For some reason or another the cast system is basing it’s hit detection off the original setup of the collider.

Problem solved.

I realized that after the map was created the platforms where being detected just find using the RayCast.
I then was wondering if this was some type of timing issue. Information about the platforms size and location was not being updated. When I needed them to be updated.

I found this post Force update of Physics Engine ? - Questions & Answers - Unity Discussions questioning the Force update of Physics Engine ?.

I then realized that maybe I might need a ForceUpdate cycle to completed to force a recalculation of the platforms. In theory after that I would be able to detect the platform correctly using the Physics2D.RayCast. I then moved all the level creation code into the FixedUpdate routine. I broken this down into small code groups and created a phase system. So basically the first phase runs. When that phase is done it lets FixedUpdate know that the next phase needs to be run. This process continues until all phases have been executed.

I am happy to say this fixed the problem.

Here is the final result. The Green lines on the right show that both columns detected a platform above where the jump assist would be placed. The player would have a hard time using the jump assist to access the platform to the right so the jump assist is not added. The Red and Green lines on the left show a single platform hit. The player would not be restricted when using the jump assist so the platform is added.

1 Like