- Home /
Moving rigidbodies with MovePosition/MoveRotation outside of FixedUpdate
I'm working on an authoritative networking scenario inspired by Valve's multiplayer networking papers, specifically the part about collecting snapshots of collider positions/rotations and rewinding them at the moment of calculating a fired shot to it's position in time equal to Network.time - transitTime - clientViewInterpolation.
I have it working fine and dandy, but moving colliders around just based on transforms with colliders and no rigidbodies is a very expensive process, due to collision data needing to be recalculated. To that end, I changed all my "hitbox" collider objects to be kinematic rigidbodies, and am doing the rewind with MovePosition/MoveRotation... it performs much better, but it happens exactly when the RewindHitBoxes() function is called, which needs to happen in the exact frame that the shot is being simulated, right before the raycast.
It's been working great, I just feel like I'm doing something dirty, not operating within FixedUpdate, but I can't afford to wait for the next physics step. Is there any unforeseen consequences of what I'm doing that I might run into?
Answer by Bunny83 · Jul 27, 2012 at 12:52 PM
There should be no problem to move it in Update. FixedUpdate is needed for accelerated movement because acceleration can't be linearly scaled. Moving at a constant speed is no problem in Update. Keep in mind that FixedUpdate is called right before Update. It is never called in between. Unity's scripting runtime runs only one thread and all code is executed between visual frames. Here's how FixedUpdate works ;)
Thanks bunny. So, a lot of people seem to claim that even if you use rigidbody.$$anonymous$$ovePosition/$$anonymous$$oveRotation outside of FixedUpdate(), it will still wait for the next physics step. What you're saying seems to imply that is untrue, correct?
Well, i never tried it myself since i don't really use $$anonymous$$ovePosition ;). It's of course possible that Unity just registers the call and processes it in the physics step but afaik it's executed immediately like Rigidbody.SweepTest
This 8 year old post came up in a search for me, so for anyone seeing this now:
For Rigidbody2d.$$anonymous$$ovePosition (2D)
"It is important to understand that the actual position change will only occur during the next physics update therefore calling this method repeatedly without waiting for the next physics update will result in the last call being used. For this reason, it is recommended that it is called during the FixedUpdate callback."
For Rigidbody.$$anonymous$$ovePosition (3D)
"Unity moves a Rigidbody in each FixedUpdate call. The position occurs in local space. Teleporting a Rigidbody from one position to another uses Rigidbody.position ins$$anonymous$$d of $$anonymous$$ovePosition... If the rigidbody has isKinematic set to false, it works like transform.position=newPosition and teleports the object to the new position (rather than perfor$$anonymous$$g a smooth transition)."
Answer by Max_power1965 · Jan 17, 2016 at 05:31 PM
For a 2D objects, You should move it inside the FixedUpdate even if it's kinematic:
From http://docs.unity3d.com/ScriptReference/Rigidbody2D.MovePosition.html " It is important to understand that the actual position change will only occur during the next physics update therefore calling this method repeatedly without waiting for the next physics update will result in the last call being used. For this reason, it is recommended that it is called during the FixedUpdate callback. "
Answer by ScroodgeM · Jul 27, 2012 at 12:31 PM
if you use MovePosition/MoveRotation just for move rigidbody to the point where object was at frame you fire, then it's much better (IMHO) to create a separate collider/trigger at this point at try to hit it instead of hitting rigidbody's collider with rigidbody moving. then, if hit was succesfull, apply some actions to rigidbody
you can use single collider for these detections - just move it every time you calc a hit
Your answer
Follow this Question
Related Questions
Rigidbody MovePosition clipping 1 Answer
Inconsistency in Rigidbody.MovePosition 0 Answers
Game not running properly on slow machines (low fps) 1 Answer
Rigidbody velocity limiter 0 Answers