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

Bug [Solved] Physics Raycast reports no hits with layer

Discussion in 'Physics' started by PompousRh0mbus, Jul 27, 2022.

  1. PompousRh0mbus

    PompousRh0mbus

    Joined:
    May 27, 2021
    Posts:
    3
    Hi there. First-time poster. Apologies for any confusing verbiage.

    So I am currently working on a program that uses raycast to detect if the player is looking at an interactable object and places a dot on the center of the screen. I had already implemented a version of the code that does this, but decided that it might be beneficial to incorporate layers in the process so that the code will only run when the player is for sure looking at an object that can be interacted with. Here is the code I have:

    Code (CSharp):
    1.         Ray ray = camera.ScreenPointToRay(Input.mousePosition);
    2.         Debug.DrawRay(ray.origin, ray.direction * 10f, Color.red);
    3.  
    4.         // Casts the ray and get the first game object hit
    5.         bool didHit = Physics.Raycast(ray, out RaycastHit hit, 5, interactables);
    6.  
    7.         if (Input.GetMouseButton(0) && didHit)
    8.         {
    9.              //interact with the object
    10.         }
    11.         if(didHit)
    12.         {
    13.             //dot appears on players screen when looking at something they can click on
    14.             cameraDot.SetActive(true);
    15.         }
    16.         else
    17.         {
    18.             //dot disappears otherwise
    19.             cameraDot.SetActive(false);
    20.         }

    Common bugs I've ruled out include:
    • Not setting the layer of the object you want to hit (I've checked this one probably 20 times by now)
    • Not setting the LayerMask variable (interactables)
    • Using LayerMask parameter in maxDistance (this one is a common mistake, but I made sure that there was a value for that parameter.
    • Ray is not hitting the object (I added a Debug ray and it very clearly is hitting the interactable objects).

    Information that may be helpful:
    • This code snippet is called in Update().
    • This code was working until I added the parameter for layermasks (dot was on screen when the ray hit colliders).
    • Unity version 2020.3.25f1

    I have been searching online for the past few hours but have found nothing. Any advice or help would be much appreciated!
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    A few things I immediately think when looking at your code
    • What does your interactables variable look like? If you set it in code, you should know that it's read bitwise, and not just the number of your layer.
    • Is your other object within 5 units away? You set the maxDistance to only 5.
    Could have been that your object is a Trigger and I know there's some settings you can change to affect that behavior, but I doubt it.
     
  3. PompousRh0mbus

    PompousRh0mbus

    Joined:
    May 27, 2021
    Posts:
    3
    Thank you for the reply. As for the solutions you provide:
    • The LayerMask variable "interactables" that is called in the script is set in the inspector, and I've checked that multiple times, so, unless there is something wrong with setting a LayerMask in the inspector, that is not the issue.
    • I have moved the character all the way so that it is touching an object on the interactables layer. I have also changed the distance to be anywhere from 5 units to over 1000 units away when debugging.
    Honestly I am stumped. I have had issues where I needed to take a break and step away before tackling it again, but this issue seems to persist.

    Thanks again for the suggestions!
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Your second answer worries me a little. The 5 you put in is the maximum distance, meaning that the Raycast will not check further than 5 units away.
     
  5. PompousRh0mbus

    PompousRh0mbus

    Joined:
    May 27, 2021
    Posts:
    3
    Solution has been found! I gotta say I feel really dumb about this mistake.

    For anyone wondering, you know how I mentioned that I checked to make sure the right objects were on the interactables layer? Well, it is true that I checked it, but what I didn't do was make sure the objects were still on that layer during run time. The objects I mentioned are prefabs that instantiate during gameplay. For some reason, when the object is instantiated, the object changes its layer to the default layer (probably because it's parent is on the default layer). To fix this issue I was having, I simply added a script to the game objects that would set their layer to the correct layer during game.

    Thanks for the help everyone!