Search Unity

Can't cast Raycast 2D properly

Discussion in '2D' started by Vaspix, Jan 26, 2020.

  1. Vaspix

    Vaspix

    Joined:
    Jun 8, 2019
    Posts:
    54
    Hello,
    I want to cast 4 rays from each side of transform, but it doesn't work and all 4 drawn lines are pointed to Player (which collided with the transform a frame before that code)
    Something is probably written wrong but idk what is happening (I'm trying to pass values in Raycast for origin, direction, and max length. And in DrawLine I'm passing origin and direction)

    Code (CSharp):
    1. RaycastHit2D hit1 = Physics2D.Raycast(transform.position, Vector2.up, 1f);
    2.  
    3.         if (hit1.transform.name == "BigBricks")
    4.             Destroy(hit1.transform);
    5.  
    6.         RaycastHit2D hit2 = Physics2D.Raycast(transform.position, Vector2.right, 1f);
    7.  
    8.         if (hit2.transform.name == "BigBricks")
    9.             Destroy(hit2.transform);
    10.  
    11.         RaycastHit2D hit3 = Physics2D.Raycast(transform.position, -Vector2.up, 1f);
    12.  
    13.         if (hit3.transform.name == "BigBricks")
    14.             Destroy(hit3.transform);
    15.  
    16.         RaycastHit2D hit4 = Physics2D.Raycast(transform.position, -Vector2.right, 1f);
    17.  
    18.         if (hit4.transform.name == "BigBricks")
    19.             Destroy(hit4.transform);
    20.  
    21.         Debug.DrawLine(transform.position, hit1.transform.position);
    22.         Debug.DrawLine(transform.position, hit2.transform.position);
    23.         Debug.DrawLine(transform.position, hit3.transform.position);
    24.         Debug.DrawLine(transform.position, hit4.transform.position);
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    It's obvious that you mean your code isn't working but you don't state clearly exactly what isn't working.

    Yes and you get the hit result but you don't check to see if you hit something; you're just assuming you do. You must always hit something because RaycastHit2D members like "Transform" will be null if you hit nothing and you'll get a NullReferenceException.

    You should always check it's a valid hit first like so:
    Code (CSharp):
    1. RaycastHit2D hit1 = Physics2D.Raycast(transform.position, Vector2.up, 1f);
    2. if (hit1 && hit1.transform.name == "BigBricks")
    Also, comparing names like that is prone to errors (typos etc) so I'd recommend placing these things on layers if you can and don't have too many of them (32 layers available in total). That way you can perform a raycast filering by layer so you don't need to check the name every time.

    Not so. You're passing the Transform position you're raycasting from to the transform position of the GameObject that had the 2D Collider on it. Note that you're not drawing a line that matches your ray query if that's what you want i.e the drawn line won't stop at the position the ray hit which is "hit.point".
     
    Last edited: Jan 26, 2020