Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

RaycastAll returns different results than Collider.Raycast

Discussion in 'Physics' started by wojciechsura, Jun 7, 2018.

  1. wojciechsura

    wojciechsura

    Joined:
    Apr 3, 2018
    Posts:
    3
    I'm having serious issues with RaycastAll not hitting all colliders on its path. The simple example:

    Code (CSharp):
    1. RaycastHit[] hits = Physics.RaycastAll(ray, float.PositiveInfinity);
    2. // DEBUG
    3. string debug = "Raycast all gave " + hits.Length + " hits:";
    4. for (int i = 0; i < hits.Length; i++)
    5.      debug += hits[i].collider.gameObject.name + "; ";
    6. Debug.Log(debug);
    7. var colliders = GameObject.FindObjectsOfType<Collider>();
    8. foreach (var collider in colliders)
    9. {
    10.      RaycastHit temp;
    11.      if (collider.Raycast(ray, out temp, float.PositiveInfinity))
    12.      {
    13.          Debug.Log("Manually verified, that ray hits " + collider.gameObject.name);
    14.      }
    15. }
    16. // END DEBUG
    The results I'm getting (one of them):

    Code (csharp):
    1. Raycast all gave 1 hits:MissileExplosion(Clone);
    2. Manually verified, that ray hits MissileExplosion(Clone)
    3. Manually verified, that ray hits Falcon(Clone)
    Note, that origin of ray is (in this case) not inside any of colliders. The game is 2,5D, camera floats above plane of action, there's no way it may finish up inside any of colliders.

    The thing that may be important is that timeScale during the presented scenario is 0 (physics is paused).

    Can timeScale being 0 cause such issue? How can I resolve it, should I resign from using RaycastAll? Is it a bug in Unity libraries?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    766
    This is puzzling. I personally haven't used Collider.Raycast (versus Physics.Raycast). Looking it up, the former will only ever hit the given collider, as long as the ray intersects it. Do you get more consistent behavior if you replace Collider.Raycast with Physics.Raycast? Meaning, do you at least only hit the MissileExplosion, and not hit the Falcon in that case?

    So far I don't have any ideas as to why your falcon would be treated differently. If you put a few missiles in the ray's path, does RaycastAll hit all the missiles? Just trying to narrow down whether this is an issue specific to your Falcon collider. Could you post a screenshot of what your Falcon collider looks like? Or, the whole game object, so we can see the layer its on?
     
  3. wojciechsura

    wojciechsura

    Joined:
    Apr 3, 2018
    Posts:
    3
    About Physics.Raycast - I might try, but I doubt it will give valid results in this case (especially since Physics.RaycastAll seems to behave incorrectly). Collider.Raycast tests only one specific collider against the ray, whereas Physics.Raycast tests all colliders and then returns information about one hit collider only (presumably, first on the path of the ray). I guess Physics.Raycast does the same as Physics.RaycastAll, but returns only one hit collider instead of all of them.

    The ray I'm talking about is actually a "mouse ray" - originating from the camera through screen to clicked place. I might put missiles there, though that would have to be artificial, since objects would never get there as game is 2.5D (gameplay takes place on a OXZ plane).

    I posted a question on Stackoverflow as well - there are more details about this issue, including Falcon's collider shape and a few screenshots, could you please take a look there?

    https://gamedev.stackexchange.com/questions/159542/why-raycastall-misses-some-objects
     
  4. wojciechsura

    wojciechsura

    Joined:
    Apr 3, 2018
    Posts:
    3
    I filed a bug report and Unity QAs managed to reproduce it. It have been sent do Unity devs and hopefully will be fixed in upcoming release of the editor.