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. Dismiss Notice

Resolved Raycast on a Rigidbody stops working after over an hour of play time

Discussion in 'Physics' started by leeham9, Nov 9, 2022.

  1. leeham9

    leeham9

    Joined:
    Oct 25, 2015
    Posts:
    23
    So, as the title says, I'm running into an issue where raycasting onto a rigidbody object seems to stop working after over an hour (I don't have an exact number) of play time.

    Context:

    I have some interactable items (gameobjects) with Rigidbodies on them. I raycast to check if the player is looking at them, and will display some text that displays the item's name. The player can then use the LMB to pick up the item.

    However, I've run into an issue where after over an hour of playtime the raycast on these items stops working.

    I've done some experimenting in the Editor, and noticed the following -- after the raycast stops working:

    • If I remove the Rigidbody entirely, the raycast will work correctly
    • If I disable + re-enable the Box Collider on the item, then the raycast will temporarily work for a few seconds, before it stops working again
    • I have some code which sets the Rigidbody to be Kinematic (and Use Gravity = false), however this doesn't fix the issue
    • I can re-create the issue by setting the Time.scale = 100, and waiting around for a few minutes... so replication at least isn't completely painful

    I'm kind of not sure the best way to proceed for solving this issue. Maintaining the Rigidbody + Raycast is vital to the game's experience.

    Is this an issue someone else has run into before? Is there some physics settings that might fix this? Or is this going to be a complex issue to solve?

    EDIT: I am using Unity 2021.3.4f1, if that's useful at all.

    Appreciate any help or advice!
     
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    leeham9 likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    I'm not in the 3D physics team but sleeping should have no effect on scene queries, that wouldn't make sense making them invisible to save time. Sleeping is about saving time during the simulation step.

    That said, if this is indeed what is happening then it's a terrible bug in PhysX.
     
    leeham9 likes this.
  4. leeham9

    leeham9

    Joined:
    Oct 25, 2015
    Posts:
    23
    Thanks for the suggestion! Unfortunately, I played around with the WakeUp function and sleep threshold, and it did not seem to fix the issue.

    Even if the Rigidbody is in the "Awake" sleep state, the Raycast continues to ignore the Rigidbody. Calling WakeUp after the Raycast issue occurs also didn't fix the issue, etc.

    I have a few things with the Physics settings I'm going to explore, then I may have to just try replicating in a blank project/empty scene to confirm if maybe it's a plugin or some stray bit of code causing this.
     
  5. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    Ah, I'm sorry, that was all I could think of. Hopefully someone else can help!
     
    Last edited: Nov 10, 2022
  6. leeham9

    leeham9

    Joined:
    Oct 25, 2015
    Posts:
    23
    No worries! Appreciate the help regardless!
     
  7. leeham9

    leeham9

    Joined:
    Oct 25, 2015
    Posts:
    23
    As an update, I think I have found the actual cause of the issue:

    There were 2 or 3 Rigidbodies in the scene which were displaying this warning beneath the Transform if viewed on the inspector:

    "Due to floating-point precision limitations, it is recommended to bring the world coordinates of the GameObject within a smaller range"

    If I set these to kinematic, and change the transform position (so they are within floating-point limitations) then Raycasting onto Rigidbodies works successfully for *all* Rigidbodies.

    Currently just working on a solution for auto-detecting and correcting any floating-point issues.

    I'll update this thread if I think the solution I come up with is useful.

    This might just be a hyper-specific issue on my end, but hopefully it can help someone else!
     
  8. leeham9

    leeham9

    Joined:
    Oct 25, 2015
    Posts:
    23
    Okay, just leaving this additional info here in case it helps anyone else:

    As it turns out, there were 3 Rigidbodies which had managed to fall out of the map. As a result, they just kept falling forever, where eventually their Y axis on the transform would end up causing an issue with floating-point precision.

    I actually had already created a script which checked for if "Items" fell out of bounds, however, the Rigidbodies which fell off the map were *not* "Items", so my script wasn't considering these specific Rigidbodies.

    I've updated my script so it can keep track of *all* Rigidbodies, so if they fall out of map, they'll respawn at their origin and have their Rigidbody set to be Kinematic.

    My solution might not work for everyone (ie: maybe Kinematic needs to be off in someone else's project), but it seems to work fine for me!

    Hopefully that helps anyone else running into a similar issue.
     
    Last edited: Nov 10, 2022
    AlTheSlacker likes this.
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    No, that's indicating that you've let them move to huge coordinate positions i.e the Transform position is huge. You don't need to work on precision issues, don't let them stray too far. This is a Transform thing. Setting it to Kinematic doesn't change that, sounds like you're just not letting them fall out of the scene via gravity or something.

    This can often be seen when Rigidbody continuing moving out of the view as a bug and end up a huge distance away.
     
  10. fredholmsimon

    fredholmsimon

    Joined:
    Sep 5, 2020
    Posts:
    92
    Thanks, @leeham9 , it appears I had some blood splatter escaping the map and falling endlessly because they never hit anything. Which made my normal shooting ray casts go through enemies. Wow. Thanks.

    I never had any warnings pop up in the console feed. The only warning appearing is if I actually select the objects in the inspector.