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

Question - Raycast only works in Editor

Discussion in 'Scripting' started by AxisRob, Nov 29, 2015.

  1. AxisRob

    AxisRob

    Joined:
    Aug 6, 2013
    Posts:
    33
    So, I have a raycast that looks like this:

    Ray ray = cachedCamera.ViewportPointToRay(new Vector3(.5f, .45f, 0));
    RaycastHit hit = new RaycastHit();
    if (Physics.Raycast(ray, out hit, 500, layerToIgnore.value) && hit.point != focalPoint) {
    focalPoint = hit.point;
    focalPointAssigned = true;
    }

    So, weird time. If I use this as is in an editor build, it is perfect. If I use this as is in an actual build, it doesn't focus at all, or the focusing makes no sense (I.e. look in a weird direction and blocks suddenly focus).

    But then if I remove the distance and layer mask, it works like a charm (this does not work for me, however, because I need to be able to ignore certain things).

    Is this a known bug?
     
  2. AxisRob

    AxisRob

    Joined:
    Aug 6, 2013
    Posts:
    33
    A quick point - the layermask is properly inverted to ignore, and runs excellently in the editor.