Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Physics2D.Raycast doesn't behave as expected.

Discussion in '2D' started by Punfish, Dec 20, 2014.

  1. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    327
    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.



    Code (csharp):
    1.  
    2.         //player.position starts at the center of the player so by getting
    3.         //approximately half of player height and width I know how far to go
    4.         //out to pass the collision box of the player.
    5.         //I get a little more height than width because I want to drop JUST below
    6.         //the bottom of the players collision box.
    7.         float playerHeightHalfed = (_characterHeight * 0.51f);
    8.         float playerWidthHalfed = (_characterWidth * 0.50f);
    9.         Vector2 playerPosition = new Vector2(transform.position.x, transform.position.y);
    10.  
    11.         Vector2 rayStart1 = new Vector2(playerPosition.x - playerWidthHalfed, playerPosition.y - playerHeightHalfed);
    12.         Vector2 rayStart2 = new Vector2(playerPosition.x + playerWidthHalfed, playerPosition.y - playerHeightHalfed);
    13.  
    14.         #region debug show expected raycast start and finish
    15.         //this code draws lines where the raycast are expected to start, and in the direction
    16.         //they are expected to be casting.
    17.         Vector3 lineStart, lineEnd;
    18.         //line left of player
    19.         lineStart = new Vector3(rayStart1.x, rayStart1.y, 0);
    20.         lineEnd = new Vector3(rayStart1.x, -1, 0);
    21.         Debug.DrawLine(lineStart, lineEnd, Color.red);
    22.         //line right of player
    23.         lineStart = new Vector3(rayStart2.x, rayStart2.y, 0);
    24.         lineEnd = new Vector3(rayStart2.x, -1, 0);
    25.         Debug.DrawLine(lineStart, lineEnd, Color.red);
    26.         #endregion
    27.  
    28.         //where raycast aims
    29.         //_vector2Down = new Vector2(0, -1)
    30.         RaycastHit2D hit = Physics2D.Raycast(rayStart1, _vector2Down);
    31.         Debug.Log("hit distance " + hit.distance);
    32.  
     

    Attached Files:

  2. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    327
    Might be worth noting that when I manually move the player just barely through the floor the value of 0 is shown.
     
  3. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,148
    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!
     
  4. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    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;
    http://overdevelop.blogspot.co.uk/2013/12/2d-platformer-collision-detection-in.html

    http://forum.unity3d.com/threads/unity-tutorial-creating-a-scroller-platformer-game.186908/
     
    Punfish likes this.
  5. K1kk0z90_Unity

    K1kk0z90_Unity

    Joined:
    Jun 2, 2013
    Posts:
    90
    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. :)
     
    Punfish and OboShape like this.
  6. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    ah, nice one K1kk0z90_Unity, that clears it up. I must be getting mixed up, apologies :(
     
  7. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,148
    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).
     
  8. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    is the direction of line 30 just commented out for testing?
    does the direction of '-Vector2.up' give the same results
     
  9. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    327
    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 ...

    Code (csharp):
    1. float playerHeightHalfed = (_characterHeight * 0.51f);
    to...
    Code (csharp):
    1. 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.
     
    OboShape likes this.
  10. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Glad you got it all working Distul.