I have a project that is using its own ‘physics engine’.
During the Update() procedure, I run all of the collision detection, using Physics.Raycast.
Simply put, I am moving objects step by step, and testing for collisions using Raycast at each stage.
The problem I have is concerning the note below, which is found in the documentation to Physics.Raycast :
Notes: This function will return false if you cast a ray from inside a sphere to the outside; this in an intended behaviour. If you move colliders from scripting or by animation, there needs to be at least one FixedUpdate executed so that the physics library can update it’s data structures, before a Raycast will hit the collider at it’s new position.
Our function is missing some collisions it ought to detect ; I believe it is because we are moving objects, and hence the colliders, then checking for collisions. The unity physics engine doesn’t move the collider straight away, but waits for the next fixedUpdate().
This fixedUpdate() occurs after our code has stepped through, so the collision doesn’t occur. I have tested exhaustively and found that if i step through the debugger until the fixedUpdate runs then the collision is detected.
Ok, so the question is , is there any way to force the Physics engine to ‘update it’s data structures’ ???
This is an answer to a super old question, but I had a similar issue recently, and found a solution. Since this is the first thing that showed up for me in google for “unity force physics recalculate”, I figured that I should post the solution here.
So, according to the docs on Physics.Raycast, “If you move colliders from scripting or by animation, there needs to be at least one FixedUpdate executed so that the physics library can update it’s data structures, before a Raycast will hit the collider at it’s new position”
First of all, this means that you cannot move a thing and then raycast against it in the same frame. This also means that you cannot create a mesh collider with a custom mesh editor time, and then raycast against it.
The solution? transform.Translate! Whenever you move a static collider, you force Unity to recalculate the scene graph. This is generally a bad thing that you should not do, but in this case it makes sense. The best thing? It works with transfrom.Translate(Vector3.zero)! So you don’t actually have to move the thing at all.
I’d still generally recommend against doing this too often at runtime, as the cost of recalculating the scene graph is very high. But, if you’re making colliders editor time, this is pretty much the only way to raycast against them without going into playmode first. If you’re doing something insane along the lines of what the OP is doing, this will also be a solution.
Since the question got already bumped I’d like to add that Unity recently added support for disabling the automatic physics simulation and added the Physics.Simulate method. Though note that this only takes care of the internal physics system. FixedUpdate will not be called when you call Simulate. Also even when you disable automatic simulation, FixedUpdate will still be called at the specified rate. Just no physics update will happen unless you call Simulate.
Note that a lot things in the Unity physics system is frame rate based, that’s why it usually runs on a fixed call count (default: 50 calls per second or in other words fixedDeltaTime == 0.02)