- Home /
VR: Using rigidbody for movement, and getting fixed time in sync with frame rate
I have a rigidbody that my VR camera rig is attached to. To move the player I add force to the rigidbody. For this to be smooth I have to set FixedDeltaTime to 1 / VR refresh rate. (I'm controlling it in FixedUpdate and using Interpolate on the rigidbody)
When I do just this it becomes silk smooth most of the time, but every 3-6 minutes it starts to judder badly for about 10 seconds, then goes back to being smooth again. The scene is quite basic, and I can see from the oculus debug tool, and unity profiler that this is not performance related. It seems to be a timing issue with fixed time. By debugging I can see that when it judders, it is doing 2 updates, then 2 fixedUpdates repeatedly (and 1 of each when smooth).
After experimenting I tried setting fixedDeltaTime to deltaTime at the beginning of an Update(). This seems to sync them up. I don't get the judder and with Interpolate set to Extrapolate on the rigidbody, it's nice and smooth.
This doesn't feel like the best solution though. I will be adding multi player soon. Will my fixed time being variable from frame to frame cause issues down the line?
I suspect that the problem here might be because the refresh rate of the headset is 90. 1 / 90 is 0.0111111111111111. But unity rounds this to 8 decimal places (0.01111111). This must be a common problem but I can't find much about it online.
Is there a better way I can sync fixedDeltaTime to always be in time with the frame rate, but with it still being constant?
Answer by rh_galaxy · Nov 01, 2021 at 11:32 PM
In my VR-game (supports Quest, Rift, and SteamVR) i have Fixed Timestep set to 0.01 (100Hz), and SteamVR does what you do by default, having a variable fixed update, which even sounds wrong.
You have to do:
SteamVR.settings.lockPhysicsUpdateRateToRenderFrequency = false;
I'm not having multiplayer (yet), but I do have a replay-recording and playing functionality which demand a fixed update at a predefined value.
The index headset can run up to 144Hz, the Quest as low as 60 but also 72Hz and 80, Rift 90, Rift S 80. It's not practical to try and sync this, leave the framerate alone and set a fixed fixed update.
You must combat the choppynes in some other way.
My code in my CameraController follows at an offset a ship (player) with a Rigidbody2D attached, and it works very smooth, no sign of the problem you are experience. But I don't know exactly why it works, maybe Interpolate and LateUpdate does the trick(?).
GameObject oPlayer; //has RigidBody2D attached to it
private Vector3 vCamOffset;
private Vector3 vMapSize;
internal static Vector3 vCamPos = new Vector3(0, 0, -4.5f);
public void InitForGame(GameLevel i_oMap, GameObject i_oPlayer)
{
oPlayer = i_oPlayer;
oMap = i_oMap;
vCamPos = new Vector3(0, 0, -10.0f); //set it away from the player,
// transform.position will then be set first Update
vCamOffset = new Vector3(0, 0.3f, -1.90f);
vMapSize = oMap.GetMapSize();
}
void LateUpdate()
{
Vector3 v = oPlayer.transform.position;
float fLeftLimit = -(vMapSize.x / 20.0f) + 0.5f;
float fRightLimit = (vMapSize.x / 20.0f) - 0.5f;
if (v.x < fLeftLimit) v.x = fLeftLimit;
if (v.x > fRightLimit) v.x = fRightLimit;
float fTopLimit = (vMapSize.y / 20.0f) - 0.3f;
float fBottomLimit = -(vMapSize.y / 20.0f) + 0.5f;
if (v.y < fBottomLimit) v.y = fBottomLimit;
if (v.y > fTopLimit) v.y = fTopLimit;
v += vCamOffset; //add this after limiting
transform.position = v; //camera holder
vCamPos = v;
}
Thanks for the response. I've done some more experimenting and sure enough, running fixed time at 100Hz with my 90Hz headset is smooth enough.
However, running fixed time at 90Hz still has long moments of stuttering. Even on 91Hz it has the occasional stuttered frame. So I'm wondering if 100Hz is enough. Will that have the same problem on headsets that use 120Hz or 144Hz? I could go up to 150Hz or something, but will that be too fast for multiplayer, and will it be fine on quest?
I'm not sure if there is a need to always have fixed timestep above framerate... I don't think so but I haven't actually tried it myself. Had one user trying it on the Index with 144Hz with no complaints, but maybe he didn't notice...
How do you make the rigidbody move. Do you addforce?
Your answer
Follow this Question
Related Questions
Input and Rigidbody (Update and FixedUpdate) 0 Answers
Can't move object in VR if it has children 0 Answers
Moving in VR using planetoid-like center of gravity 0 Answers
how to have a fixed movement using rigidbodies without animations 0 Answers
I want to freeze Y position of an object while using steam VR. 1 Answer