Search Unity

Can't get Physics2D.Raycast to check if obstacle is between player and enemy

Discussion in '2D' started by Nomad_Artisan, Dec 15, 2013.

  1. Nomad_Artisan

    Nomad_Artisan

    Joined:
    Jun 29, 2013
    Posts:
    20
    I'm trying to use Physics2D.Raycast to see if there are any obstacles between an enemy and the player.

    I'm using the following line of code.

    Code (csharp):
    1. var hit: RaycastHit2D = Physics2D.Raycast(transform.position, transform.position - target.transform.position);
    This code is placed on the enemy object and the player object is the target.
    Even though the Raycast is originating at the enemy's center, the RaycastHit2D is returning the enemy's collider.
    I was under the impression that a raycast can't hit the inside of a collider, but it seems to be doing just that.

    What am i missing?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use Linecast instead, it's simpler for this.

    --Eric
     
  3. Nomad_Artisan

    Nomad_Artisan

    Joined:
    Jun 29, 2013
    Posts:
    20
    I've tried Linecast and have the same problem

    Code (csharp):
    1. var hit : RaycastHit2D = Physics2D.Linecast(transform.position, target.transform.position);
    2. print(hit.transform.tag);

    this is still printing the enemy's tag.

    any ideas?
     
  4. monkeyThunk

    monkeyThunk

    Joined:
    Oct 6, 2011
    Posts:
    13
    Did you add 2D colliders to the obstacles?

    Also, I needed to adjust the z coordinates of my sprites to get the layering to work properly, which seemed odd.
     
  5. Nomad_Artisan

    Nomad_Artisan

    Joined:
    Jun 29, 2013
    Posts:
    20
    Yes, they all have 2D coliders.
    And everything is at the same z coordinate.

    I'm baffled
     
  6. arlinon

    arlinon

    Joined:
    Sep 22, 2013
    Posts:
    1
    I'm having the same issue too :/
     
  7. dasbin

    dasbin

    Joined:
    Jan 14, 2012
    Posts:
    261
    You impression is incorrect - the 2D Raycast documentation page specifically states that the Raycast will hit inside its originating collider. It's annoying but that's the way it is.

    Solution 1: put the enemies on a separate layer and specify which layers to look for in your raycast.
    Solution 2: Use RaycastAll or LinecastAll instead, then, using a for/foreach loop, cycle through to find the target you want and ignore the ones you don't (including the enemy).

    From the Physics2D.Raycast page:
    "Additionally, this will also detect Collider(s) at the start of the ray."

    Hate to be that guy, but RTFM :)
     
    Last edited: Jan 1, 2014
  8. Caliber-Mengsk

    Caliber-Mengsk

    Joined:
    Mar 24, 2010
    Posts:
    689
    I'm going to also point out that Collider events (oncollisionenter, ontriggerenter, etc) don't run unless one of the objects have a rigidbody. I don't remember if this also pertains to raycasting, but you could try adding a rigidbody as well, if you don't have one. Also, as stated, you can hit a collider from the inside with a raycast. Happens all the time. Just add a variable at the top of the type LayerMask, and add that to the end of your raycast line. Then in the editor, select what you want it to find. Set the player to the player layer mask, and the enemies to the enemy layer mask. Then, depending on which one is doing the ray casting, simply remove them from the layermask variable in the editor. :D