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

Raycasting against a specific collider and/or a list of colliders?

Discussion in 'Physics' started by michalpiatek, Aug 3, 2022.

  1. michalpiatek

    michalpiatek

    Joined:
    Feb 26, 2021
    Posts:
    81
    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 perform a raycast against it and it only. Is there a function that does that? If not, how I would go about doing such a thing?

    And no, I can't assign this collider to a bespoke layer. In my use-case this won't do the trick. (Unless I can assign a collider to a specific layer at runtime and then remove it from that layer?)
     
  2. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    900
  3. michalpiatek

    michalpiatek

    Joined:
    Feb 26, 2021
    Posts:
    81
    Thank you kind sir.
     
  4. ExNinja

    ExNinja

    Joined:
    Dec 4, 2013
    Posts:
    29
    "And no, I can't assign this collider to a bespoke layer. In my use-case this won't do the trick. (Unless I can assign a collider to a specific layer at runtime and then remove it from that layer?)"

    Collider.Raycast() is the correct answer here, but you can assign a Collider to a specific layer at runtime by setting the collider.gameObject.layer to whatever number (0-31) that you want and then setting it back to the original layer later.