Raycast2D Stops at first object


I have this script, but the RayCast2D stops at the first object it is inside (Mining laser attached to SpaceShip), so it basically stops at Spaceship.

Vector2 direction = transform.TransformDirection(Vector2.up);
        RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, Mathf.Infinity);

        if (hit.collider != null && hit.collider.tag != "Ship_PlayerSpaceShip")
            distance = Vector2.Distance(transform.position, hit.point);
            distance = 25;
        if (distance > 25)
            distance = 25;
        gameObject.GetComponent<LineRenderer>().SetPosition(1, new Vector3(0, distance * 2, 0));

When I disable collider of that Ship, laser object(object from which raycast is cast forward in 2d space ofc) all is working fine. So for example laser colliders with asteroid and it has nice effect.

However when I turn On the Ships Collider, the raycast stops there and refuses to go further to check for other collision.

Please help I am getting crazy from that

Using a LayerMask is your best way out of this one.
As mentioned before but not replied to yet.
I quote:

I tried using the separate layer, and
it didn`t work.

If I use this, will the layer named
IgnoreTHis be ignored or only this
layer will get detected? (Syntax may
be wrong its just an example)

    RaycastHit2D hit = Physics2D.Raycast(transform.position,

The layer you put in the Layermask parameter WILL BE THE ONLY ONE HIT. :3
If you wish to ignore a layer instead, you can invert the layermask using a Squiggly ~ operator.
Not checking on the syntax either, but basically like this:

RaycastHit2D hit = Physics2D.Raycast(transform.position,

In the above code, the Raycast will hit everything BUT the layer “IgnoreThis”.
Cool tip, you can store a:

public LayerMask IgnoreLayer;

Which will allow you to easily select a cool layer in the inspector, getting rid of some dirty code, and making it much nicer to update, especially if you use the Layermask in multiple places. :slight_smile:

I hope that helps. If this answer is satisfactory, please accept it.
If not, I’ll be awaiting more questions / details :3

Best of luck!
Yours truly,

Hi, thank you for the reply :slight_smile:

I had to use raycastAll apart with normal raycast2D to get what I want, hope it will be working.
For any future problems people may have I write the code here:

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D hit01 = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);

        Vector2 direction = transform.TransformDirection(Vector2.up);
        RaycastHit2D[] hit = Physics2D.RaycastAll(transform.position, direction, Mathf.Infinity);

        if (hit01.transform != null)
            print("Mouse hitting something");
            foreach (RaycastHit2D asteroidHit in hit)
                if (asteroidHit.transform.tag == "Asteroid" && hit01.transform.tag == "Asteroid")
                     print("Hitting exactly object with tag I want");

But I joined it with mouse click since laser has to fire only on asteroids.