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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to detect collider tag using raycast?

Discussion in 'Physics' started by macktoxic, May 20, 2015.

  1. macktoxic

    macktoxic

    Joined:
    May 12, 2015
    Posts:
    4
    Can raycast detect collider tag? I want to make a script that allow my character to add fog if he hit a plane. Like if he goes underwater the camera will add some fog and distort something like that. But my problem is I have the fog script working but I don't know how to trigger or make it enable when he collides on the plane (water).

    Can anybody give me a example of a raycast hit that detect a collider tag.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    Why not just use OnTriggerEnter/Exit to turn the fog on and off?

    Or even easier, just turn it on if your player is below a certain y-level, unless you're working with different water instances on different heights.

    If you're truly insisting on using raycasts, it's not hard - just grab the RaycastHit's collider.tag:

    Code (CSharp):
    1. if(Physics.Raycast(origin, direction, out hit) {
    2.     string tag = hit.collider.tag;
    3. }
     
  3. macktoxic

    macktoxic

    Joined:
    May 12, 2015
    Posts:
    4
    yeah I will use this script only for the lake above the mountains that has a higher y level than my other water plane. I will try this and give you a feedback after. I will also attach this on the main camera.

    this is the screenshot. it didn't work.
     
    Last edited: May 22, 2015