Physics2D.Raycast doesn't behave as expected.

I’m using Physics2D.Raycast to detect when my player is touching another object, only on the bottom(feet).

Both the player and the floor are using Rigidbody 2D, and Collision 2D elements. Both share the Z plane with a value of 0.

When I issue a Raycast downwards while my player rest on the floor I expect a small value, less than 1 or 2, but instead I get a rather large value as though the raycast is going through the floor.

        //player.position starts at the center of the player so by getting
        //approximately half of player height and width I know how far to go
        //out to pass the collision box of the player.
        //I get a little more height than width because I want to drop JUST below
        //the bottom of the players collision box.
        float playerHeightHalfed = (_characterHeight * 0.51f);
        float playerWidthHalfed = (_characterWidth * 0.50f);
        Vector2 playerPosition = new Vector2(transform.position.x, transform.position.y);

        Vector2 rayStart1 = new Vector2(playerPosition.x - playerWidthHalfed, playerPosition.y - playerHeightHalfed);
        Vector2 rayStart2 = new Vector2(playerPosition.x + playerWidthHalfed, playerPosition.y - playerHeightHalfed);

        #region debug show expected raycast start and finish
        //this code draws lines where the raycast are expected to start, and in the direction
        //they are expected to be casting.
        Vector3 lineStart, lineEnd;
        //line left of player
        lineStart = new Vector3(rayStart1.x, rayStart1.y, 0);
        lineEnd = new Vector3(rayStart1.x, -1, 0);
        Debug.DrawLine(lineStart, lineEnd, Color.red);
        //line right of player
        lineStart = new Vector3(rayStart2.x, rayStart2.y, 0);
        lineEnd = new Vector3(rayStart2.x, -1, 0);
        Debug.DrawLine(lineStart, lineEnd, Color.red);
        #endregion

        //where raycast aims
        //_vector2Down = new Vector2(0, -1)
        RaycastHit2D hit = Physics2D.Raycast(rayStart1, _vector2Down);
        Debug.Log("hit distance " + hit.distance);

1893436--121905--sample1.jpg

Might be worth noting that when I manually move the player just barely through the floor the value of 0 is shown.

I can’t see anything wrong with what you’ve posted so far.

I’ve seen some issues with Physics2D castings, but mainly with BoxCast and CircleCast, although admittedly I haven’t dabbled much with the ray cast methods. If I had to guess, I’d say there is some minor error somewhere in your setup.

Can you upload a zipped project containing your setup? You may need to duplicate your existing project and remove assets to get the file size down. Obviously don’t include anything that you don’t want others to steal!

just as a quickie, as I was working with 2D raycasting over the last few months. and came across some interesting things that drove me mad for ages, when trying to get it working the way i wanted for a character sprite.

from what ive understood (hope its right), raycasts dont trigger a hit if they start inside a collider, they only trigger an entry point.
so if you start the raycast inside the player volume (almost like a skin) it wont detect itself, instead it will hit the next item it comes it contact with.

im just looking at the code briefly, are you starting the raycast slightly outwith the players lower bounds? would this then mean that the raycast would start from within the ground? if it is, then it may be not registering the hit on the ground, but the object below that.

in your raycast hit test, put a Debug.Log to see what the hit.collider.name is, might give an idea where its actually hitting.

heres a couple of pages where i got alot of info from whilst trying to learn, might be able to glean some info from these;

1 Like

Actually from my experiments it seems that the ray of the raycast also detects the collider inside of which the ray starts. The ray starts from the edge of the player’s character BoxCollider2D, and if I drag that character inside of a platform, the raycast detects that platform even if the ray starts inside of it. Anyway at the moment I’m doing these experiments inside Unity 4.3 (PSM version), so I still have to try this in the last version. :slight_smile:

2 Likes

ah, nice one K1kk0z90_Unity, that clears it up. I must be getting mixed up, apologies :frowning:

There is an option under Edit → Project Settings → Physics2D that controls whether a raycast/linecast that starts in a collider will hit that collider. It is called “Raycasts Start in Colliders.”

I’m not 100% sure about this, but I think disabling that option will stop the raycast/linecast from hitting the collider it starts in.

Still, the picture the user posted shows the box character off the ground, so I doubt this is the problem (unless the picture doesn’t accurately represent what’s happening).

is the direction of line 30 just commented out for testing?
does the direction of ‘-Vector2.up’ give the same results

Thank you for the information. Sorry for my delay in response, been busy with work.

I think it is an issue with the raycast colliding with the location in which it came from. Oddly enough, with my code the raycast SHOULD start outside the casting collider but after testing it is not. I changed …

float playerHeightHalfed = (_characterHeight * 0.51f);

to…

float playerHeightHalfed = (_characterHeight * 0.55f);

and everything worked fine after.

Yes, the comment on line 29 is just to show my variable values so you know what I’m doing.

1 Like

Glad you got it all working Distul.