There are several approaches to your problem.

1) Cast your ray inside the player

Raycast() will ignore a collider if the ray starts inside it. Lets say your player is a cube at position (0,0,0) with a scale of (1,1,1). A ray from (0,0,0) with direction (1,0,0), i.e., along the x-axis, will not hit the player. If you move the ray's starting position to (-2,0,0), the ray will hit the player.

This may get complicated if your player consists of several collider and you want to cast the ray into different directions. You could literally shoot yourself in the foot (with a ray starting in the head) :)

2) Use layers

You can assign game objects to different layers. Then you can specify a layer mask and tell Raycast() the layers you want to hit.

A step-by-step tutorial to create and assign layers can be found in the manual. If you followed the steps and set layer #8 as your player layer, the following code will ignore all objects in the player layer.

// Bit shift the index of the layer (8) to get a bit mask
int layerMask = 1 << 8;

// This would cast rays only against colliders in layer 8, so we just inverse the mask.
layerMask = ~layerMask;

if (Physics.Raycast(transform.position, fwd, 10, layerMask)) {
    // hit something not in the player layer
}

3) Get all colliders that intersect the ray

By using RaycastAll() you get an array of all colliders that intersect with the ray. Then you can sort out the ones you don't want.

RaycastHit[] allHits;
allHits = Physics.RaycastAll(transform.position, fwd, 10);
foreach (var hit in allHits)
{
    // now filter by tag or name
    if (!hit.transform.tag.equals("Player")) {
        // yeah, we didn't hit something tagged as "Player"
    }
}
Answer from Stefan Hoffmann on Stack Overflow
🌐
Unity
discussions.unity.com › unity engine
Raycasting against a specific collider and/or a list of colliders? - Unity Engine - Unity Discussions
August 3, 2022 - I want to raycast against a specific single mesh collider but I can’t seem to find an appropriate physics method that would do that. My collider is disabled (for unrelated reasons) and should stay this way, but I want to…
🌐
Unity
docs.unity3d.com › ScriptReference › Collider.Raycast.html
Unity - Scripting API: Collider.Raycast
using UnityEngine; using System.Collections; using UnityEngine.InputSystem; public class ExampleClass : MonoBehaviour { public Collider coll; void Start() { coll = GetComponent<Collider>(); } void Update() { // Move this object to the position clicked by the mouse. if (Mouse.current.leftButton.wasPressedThisFrame) { Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue()); RaycastHit hit; if (coll.Raycast(ray, out hit, 100.0f)) { transform.position = ray.GetPoint(100.0f); } } } }
Discussions

Why can't I use a raycast while standing inside a collider?
I know it says on the docs that “Raycasts will not detect Colliders for which the Raycast origin is inside the Collider.” But even when I alter the origin so it is clearly above and outside the collider and is not detecting any other collider, it still does not work. void Update() { int ... More on discussions.unity.com
🌐 discussions.unity.com
0
0
October 15, 2023
c# - Raycast but ignore the collider of the gameobject it's being called from - Stack Overflow
This makes it so that you do not ... raycasting). It just ignores the collider on the gameobject casting the ray, so you can have multiple prefabs of players/enemies casting rays and hitting each other, but not themselves. ... I think this setting may have changed to 'Queries Start in Colliders' (in Unity ... More on stackoverflow.com
🌐 stackoverflow.com
Raycast from inside a collider
I would just do a very small Physics.OverlapSphere at the ray origin More on reddit.com
🌐 r/Unity3D
16
5
August 5, 2025
Is there any way to get the collider of a raycast? - Unity Engine - Unity Discussions
Hey there, as title says I just want to know if there’s a simple way of getting the collider of a raycast that you have shot out. Not the collider of the gameobject it hits, but the collider of the raycast itself. I would like to use this to make it able to use the ignore.physics function. More on discussions.unity.com
🌐 discussions.unity.com
0
May 10, 2022
🌐
Unity
docs.unity3d.com › 6000.3 › Documentation › ScriptReference › Physics.Raycast.html
Unity - Scripting API: Physics.Raycast
Casts a ray against all colliders in the Scene and returns detailed information on what was hit. This example reports the distance between the current object and the reported Collider: using UnityEngine; public class RaycastExample : MonoBehaviour ...
🌐
Unity
discussions.unity.com › unity engine
Why can't I use a raycast while standing inside a collider? - Unity Engine - Unity Discussions
October 15, 2023 - I know it says on the docs that “Raycasts will not detect Colliders for which the Raycast origin is inside the Collider.” But even when I alter the origin so it is clearly above and outside the collider and is not detecting any other collider, it still does not work. void Update() { int ...
Top answer
1 of 6
40

There are several approaches to your problem.

1) Cast your ray inside the player

Raycast() will ignore a collider if the ray starts inside it. Lets say your player is a cube at position (0,0,0) with a scale of (1,1,1). A ray from (0,0,0) with direction (1,0,0), i.e., along the x-axis, will not hit the player. If you move the ray's starting position to (-2,0,0), the ray will hit the player.

This may get complicated if your player consists of several collider and you want to cast the ray into different directions. You could literally shoot yourself in the foot (with a ray starting in the head) :)

2) Use layers

You can assign game objects to different layers. Then you can specify a layer mask and tell Raycast() the layers you want to hit.

A step-by-step tutorial to create and assign layers can be found in the manual. If you followed the steps and set layer #8 as your player layer, the following code will ignore all objects in the player layer.

// Bit shift the index of the layer (8) to get a bit mask
int layerMask = 1 << 8;

// This would cast rays only against colliders in layer 8, so we just inverse the mask.
layerMask = ~layerMask;

if (Physics.Raycast(transform.position, fwd, 10, layerMask)) {
    // hit something not in the player layer
}

3) Get all colliders that intersect the ray

By using RaycastAll() you get an array of all colliders that intersect with the ray. Then you can sort out the ones you don't want.

RaycastHit[] allHits;
allHits = Physics.RaycastAll(transform.position, fwd, 10);
foreach (var hit in allHits)
{
    // now filter by tag or name
    if (!hit.transform.tag.equals("Player")) {
        // yeah, we didn't hit something tagged as "Player"
    }
}
2 of 6
16

I recently had this problem. It was a pain to figure out, yet it is a simple fix.

I found that if you go to Edit -> Project Settings -> Physics2d and Uncheck the box that says "Raycasts Start In Colliders" it solves this issue.

This makes it so that you do not have to deal with layers (I find layers to be a very impractical way of selectively raycasting). It just ignores the collider on the gameobject casting the ray, so you can have multiple prefabs of players/enemies casting rays and hitting each other, but not themselves.

🌐
Reddit
reddit.com › r/unity3d › raycast from inside a collider
r/Unity3D on Reddit: Raycast from inside a collider
August 5, 2025 -

Hello!

I am looking for ways to raycast from the inside of a collider or trigger.

As the documentation says, rays starting inside a collider will not detect said collider - but I require some method to do it, so as to keep the code clean, robust and reliable.

I know there's the `Physics.queriesHitBackfaces` option, but that doesn't work for non-mesh colliders.

I know it's possible to trace the ray backwards (from the target position towards the player), but that doesn't account for larger colliders and can lead to hitting a different collider altogether.

How else can I detect the ray already begins within the collider it can hit?

I mainly require this to improve the gameplay experience, for instance:

- Player interacts with a ladder; By pressing E on it, they get attached, and by pressing E again they detach. During the attachment process, the player is moved slowly into the ladder's trigger, so pressing E while looking anywhere should detach the player (I don't want them to pixelhunt for the ladder entity again). This can be accomplished by caching the ladder entity when it is hit and if player presses E while in an 'attached' state they get detached, but again you can see that this is not robust nor stateless at all, compared to using the same system for attachment/detachment.

- Player is inside 'water' trigger; firing their gun from the inside of the water has a different effect on the bullet than firing it from outside towards it - perhaps the player can't fire while inside, making the check trivial (if (hit_distance < threshold) return; ) compared to having a boolean flag for 'isUnderwater' and then somehow estimating if the portion of the collider is below its surface, when a simple raycast could do just fine.

Thank you for any suggestions. This really feels like a major oversight, if there's no reliable way.

🌐
Unity
discussions.unity.com › unity engine
Is there any way to get the collider of a raycast? - Unity Engine - Unity Discussions
May 10, 2022 - Hey there, as title says I just want to know if there’s a simple way of getting the collider of a raycast that you have shot out. Not the collider of the gameobject it hits, but the collider of the raycast itself. I would like to use this to make it able to use the ignore.physics function.
Find elsewhere
🌐
Unity
docs.unity3d.com › ScriptReference › RaycastHit-collider.html
Unity - Scripting API: RaycastHit.collider
This property is null if the ray hit nothing and not-null if it hit a Collider. using UnityEngine; using UnityEngine.InputSystem; public class Example : MonoBehaviour { void Update() { if (Mouse.current.leftButton.wasPressedThisFrame) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue()); if (Physics.Raycast(ray, out hit)) { if (hit.collider != null) { hit.collider.enabled = false; } } } } } Additional resources: Physics.Raycast, Physics.Linecast, Physics.RaycastAll.
🌐
Unity
discussions.unity.com › questions & answers
Help With Ignoring Collider With Raycast!? - Questions & Answers - Unity Discussions
April 1, 2011 - Hello, I am using the FPS tutorial from Unity. I am using the machine gun script and I want to ignore a specific collider (Element 7 tag - want to ignore). Here is the machine gun script, I've added the layerMask, but …
🌐
Unity
discussions.unity.com › unity engine
Raycast passes through collider. - Unity Engine - Unity Discussions
September 13, 2023 - I’m making a laser beam in Unity using a raycast and a line renderer. The Raycast works great for the most part, it’s endpoint will change based on where its colliding with the enemy. However, it will sometimes pass thro…
🌐
Unity
discussions.unity.com › unity engine
Raycast does not hit collider - Unity Engine - Unity Discussions
September 14, 2020 - Hello, I have the problem of a raycast not hitting a collider and I tried everything now, so I hope someone can point something out that I am missing. As you can see, the raycast (red line) goes through the collider. Also the layer is correct. I also do a “debug” raycast which gives me all colliders hit on all layers.
🌐
Unity
docs.unity3d.com › ScriptReference › Collider2D.Raycast.html
Unity - Scripting API: Collider2D.Raycast
public int Raycast(Vector2 direction, ContactFilter2D contactFilter, RaycastHit2D[] results, float distance = Mathf.Infinity); ... Casts a ray into the Scene that starts at the Collider position and ignores the Collider itself.
🌐
Unity
forum.unity.com › unity engine
Unity Physics: Colliders or Raycast? - Unity Engine - Unity Discussions
February 18, 2019 - Hi everyone ! I am working on a game where the enemy must detect the player and do some villainous stuffs. I am confused between using colliders or raycast/spherecast. All I need is a better performance low cost solutio…
🌐
Unity
docs.unity3d.com › 6000.3 › Documentation › ScriptReference › Physics2D.Raycast.html
Unity - Scripting API: Physics2D.Raycast
A raycast is conceptually like a laser beam that is fired from a point in space along a particular direction. Any object making contact with the beam can be detected and reported. This function returns a RaycastHit2D object with a reference to the Collider that is hit by the ray (the Collider property of the result will be NULL if nothing was hit).
🌐
Stack Overflow
stackoverflow.com › questions › 66017325 › trying-to-hit-box-collider-with-raycast
c# - Trying to hit Box Collider with Raycast - Stack Overflow
I am trying to play an animation after clicking by mouse click on a box collider which is on a prefab named "TorObjekt", but nothing happens the animation isn't playing neither the debug.log. ... using System.Collections; using System.Collections.Generic; using UnityEngine; public class Tor : MonoBehaviour { private void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { if (hit.transform.gameObject.name == "TorObjekt") GetComponent<Animator>().SetTrigger("Tor"); Debug.Log("Hello?"); Destroy(hit.collider.GetComponent<BoxCollider>()); } } } }
🌐
Unity
discussions.unity.com › unity engine
Equivalent of 3D Collider.Raycast - Unity Engine - Unity Discussions
June 22, 2021 - Both 2D and 3D physics has Raycast methods on the Collider type, but they’re very different beasts: Collider2D.Raycast: “Casts a ray into the Scene that starts at the Collider position and ignores the Collider itself.” Collider.Raycast: ...