Search Unity

Physics.Raycast misses randomly

Discussion in 'Physics' started by komatii, Jun 7, 2011.

  1. komatii

    komatii

    Joined:
    Sep 8, 2010
    Posts:
    58
    I'm using Physics.Raycast on my bullets to determine if they hit something, however it doesn't always work.
    The part that makes it weird, is that I can repeatedly fire bullets at the same object, from the exact same angle and watch as most hit, but occasionally one misses.
    Other times, I can get a situation where every single bullet misses an object as the raycast fails, but if I pause the game and step through frame by frame, every bullet hits. I get this with both box colliders and mesh colliders.

    The bullet object itself has it's axis at the trailing end. The physics.raycast uses this axis point as the origin, and the position for next frame plus 5% to ensure a bit of overlap to prevent it from missing surfaces lying right at the origin.

    The raycast is done in the bullet objects Update() function.
     
  2. MattFS

    MattFS

    Joined:
    Jul 14, 2009
    Posts:
    205
    I get similar issues as well with raycast...
    I even moved all raycasting code to FixedUpdate() and still got random misses.
    It's extremely frustrating. Further to this, in Editor I get almost 0% misses which is great... but any other platform or build and there's almost a 50% hit miss...

    What could be causing this?
     
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,071
    You'll possibly want to do it in fixed update.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,200
    I've never seen a raycast miss, in the editor or a build. I don't think it's mathematically possible. The thing to watch out for in certain cases is that you need to be aware of the order in which you move rigidbodies and do raycasts, because otherwise it's possible that colliders aren't actually where they appear to be. So the raycast appears to "miss" a collider but that's actually not what's happening--it's a legitimate miss because the collider isn't really there, it's using the position from the previous physics frame, since it hasn't actually been moved yet. (Yeah, I ran into that issue a while ago, it was a bit confusing at first....)

    --Eric
     
    Regone likes this.
  5. MattFS

    MattFS

    Joined:
    Jul 14, 2009
    Posts:
    205
    ah sure, thanks for the heads up Eric I'll look into!
     
  6. rageingnonsense

    rageingnonsense

    Joined:
    Dec 3, 2014
    Posts:
    99
    I know this is an old thread, but I do see this happen. I don't think it has to do with FixedUpdate() necessarily, as I see it happen when raycasting against colliders that do not even move.

    I'm pretty sure that it can also happen due to floating point precision issues, and therefore mathematically possible due to that. It does not happen often, but it DOES happen (and very annoying when it does).

    It also happens sometimes when raycasting against a very flat collider.
     
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,071
    No, it doesn't happen. If if did, it would be classified as an urgent Unity bug to be fixed ASAP. If you're confident it is, file a bug. More likely:

    - your ray really isn't where you think it is - draw a debug ray that correctly matches it.
    - the ray begins inside a collider
    - there's colliders you're not sure of
    - you transform the ray direction and position too fast and skip

    Unless you're 10-20k units out, I doubt it's a precision issue. If you are more than 10-20k units out you'd best look at how your game is structured (start having all kinds of issues with bigger spaces, not just physics), assuming 1 meter = 1 unit with a humanoid reference scale.
     
  8. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    Actually, this is also happening with me. When I rotate my GameObject most of the time reflected ray doesn't show up.
     
    Split3 and Dr-Trax like this.
  9. Dr-Trax

    Dr-Trax

    Joined:
    Mar 16, 2018
    Posts:
    10
    It misses a lot! I have a flickering debug ray.
     
    Split3 likes this.
  10. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,071
    Is it far from the origin? You should probably post some code or try to narrow it down.
     
  11. Dr-Trax

    Dr-Trax

    Joined:
    Mar 16, 2018
    Posts:
    10
    RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.up);
    arrowLine.SetPosition(0, transform.position);
    arrowLine.SetPosition(1, hit.point);
    RaycastHit2D hit2 = Physics2D.Raycast(hit.point, Vector2.Reflect(transform.up, hit.normal));
    arrowLine.SetPosition(2, hit2.point);
     
  12. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,071
    Yeah is it in fixedupdate?
     
  13. Dr-Trax

    Dr-Trax

    Joined:
    Mar 16, 2018
    Posts:
    10
    Neither FixedUpdate nor Update is working.
     
  14. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,071
    That wasn't my question. Are you aware fixedupdate can be called at a different frequency to update, it would likely cause some debug flickering.
     
  15. Dr-Trax

    Dr-Trax

    Joined:
    Mar 16, 2018
    Posts:
    10
    I am aware of that.
    The origin of the Raycast is rotating. I tried to update the Line Renderer in both Updates and still the reflecting Material flickers the same as the Debug Ray.
     
    Last edited: Apr 1, 2018
  16. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    96

    Apparently, my Raycast isn't hitting my Capsule Collider. I have a zombie character that I enable the ragdoll (all of the collider GameObjects) once the health reaches zero. I then shoot a Raycast at it to apply force to the hit Ragdoll component, but the Raycast fails about 10 - 20% of the time. Both of these things happen in the same frame, I don't know if that could be the reason. The Raycast originates from input which is calculated in the Update Method.

    It could be just an editor thing, but one thing that troubles me is that when I call Debug.Break() in the case that the Raycast fails, It takes about half a second to show that the collider GameObject is active, and a few seconds to show the green collider outline in the scene view.

    My current solution is to try the Raycast again next frame in a coroutine which hasn't failed yet, but that is only if I can not sort this out. I also used Debug.DrawLine to see the ray in the scene view and it goes straight through the collider. My Collision Mask is set to Dead Ragdoll and a few others, and my testGo object from the debug code is set to the head.

    I have attached a screenshot of the Editor as well as my Raycast and the Debug code. Please help.

    Screenshot_1339.png Screenshot_1342.png
     
  17. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,071
    What is collisionMask's value?
     
  18. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    96
    Dead Ragdoll and Ragdoll.

    Screenshot_1351.png
     
    Last edited: Jan 25, 2019
  19. bart_the_13th

    bart_the_13th

    Joined:
    Jan 16, 2012
    Posts:
    441
    Is there any chance that the raycast starts inside any collider?
     
  20. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    96
    Definitely not at the time of the error:

    Screenshot_1350.png
     
  21. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    96
  22. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    96
    Well, that was fast, and no, unfortunately, updating to 2018.3.3f1 did not help.
     
  23. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    96
    Sorry for so many updates...

    I changed my zombie ragdoll code to not enable the zombie ragdoll GameObjects then move the ragdoll transforms to the correct positions, but instead to move the disabled ragdoll transforms to the correct positions, then enable the GameObjects and so far the error hasn't occurred.

    Could this have something to do with FixedUpdate not aligning with Update all the time and moving the collider a bit late 10-20% of the time even though the transform was in the correct position? This would explain the error only occurring sometimes.

    Edit: Apparently this causes another error with my ragdoll, but not the topic of this forum thread.

    If anyone sees this and wants to know, the solution was to move both the transform.position and ragdoll.position.
     
    Last edited: Feb 2, 2019
  24. nsfnotthrowingaway

    nsfnotthrowingaway

    Joined:
    Feb 18, 2016
    Posts:
    45
    Just in case this helps anyone, I had a problem with raycasts frequently missing and flickering as I dragged the mouse, which was casting a screenpointtoray of length float.postiveinfinity. The problem ended up being the near and far clipping planes of the camera. I had them set to an unnecessarily huge difference, and I guess the raycasts normalize somehow to the distance and introduce floating point errors (that's my hunch at least)[EDIT: better hunch in my post below]. Regardless, shrinking the distance between them to something still huge, but more reasonable gave me completely consistent and stable raycasts.
     
    Last edited: Feb 10, 2019
  25. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    96
    I couldn't imagine raycasts that long being a good idea. How big is your world?
     
  26. nsfnotthrowingaway

    nsfnotthrowingaway

    Joined:
    Feb 18, 2016
    Posts:
    45
    @chadfranklin47
    Infinity is the default for raycast length. I guess the issue is probably that ScreenPointToRay needs to take into account near clipping plane as opposed to just the z position when firing a ray, which makes sense, but wasn't intuitively the place to look when trying to troubleshoot Physics.Raycast. I was just experimenting with some 2d orthographic visualization, and I made the near and far clipping planes arbitrarily huge. The distance between the near clipping range and some of the objects in the far clipping range was introducing floating point precision issues since the objects weren't particularly far apart.
     
    Last edited: Feb 10, 2019