Skip to main content
added 454 characters in body
Source Link
Martin
  • 153
  • 1
  • 2
  • 9

I'm trying to simulate physics manualy in a Unity 3D game. I've turned off Physics "Auto Simulation" in Project Settings and wrote this piece of code (it is more or less the unity doc sample)

private void Update()
{
    this.timer += Time.deltaTime;
    while (this.timer >= Time.fixedDeltaTime)
    {
        this.timer -= Time.fixedDeltaTime;

        controller.ApplyForces(controls);
        Physics.Simulate(Time.fixedDeltaTime);
    }
}

But when I play the game, movements are lagging a lot.
In fact, I can explain it: my game runs at 120 fps. The Time.fixedDeltaTime is set to 0.01667 (60fps). So 1 out of 2 frames are not simulating physics! In other words, half of my Update calls don't enter the while loop, which produce the jump effect.

I'm sure i'm missing something... what is the recommended way to do this? Should I call Physics.Simulate in FixedUpdate?

EDIT:

As someone asked me in the comments (then deleted the comment), I'm simulating Physics manually for networking reason. With manual physics, I can do predictions and corrections. Here's a link that describes the problem

EDIT2:

Thanks to @DMGregory and his excellent answer, I figured out that the problem comes from the lack of interpolation of my player rigidbody:
InterpolationMode seems to be handled by Unity when autosimulation is set to ON, which is not my case.

So now I'm looking how to handle this myself.

I'm trying to simulate physics manualy in a Unity 3D game. I've turned off Physics "Auto Simulation" in Project Settings and wrote this piece of code (it is more or less the unity doc sample)

private void Update()
{
    this.timer += Time.deltaTime;
    while (this.timer >= Time.fixedDeltaTime)
    {
        this.timer -= Time.fixedDeltaTime;

        controller.ApplyForces(controls);
        Physics.Simulate(Time.fixedDeltaTime);
    }
}

But when I play the game, movements are lagging a lot.
In fact, I can explain it: my game runs at 120 fps. The Time.fixedDeltaTime is set to 0.01667 (60fps). So 1 out of 2 frames are not simulating physics! In other words, half of my Update calls don't enter the while loop, which produce the jump effect.

I'm sure i'm missing something... what is the recommended way to do this? Should I call Physics.Simulate in FixedUpdate?

EDIT:

As someone asked me in the comments (then deleted the comment), I'm simulating Physics manually for networking reason. With manual physics, I can do predictions and corrections. Here's a link that describes the problem

I'm trying to simulate physics manualy in a Unity 3D game. I've turned off Physics "Auto Simulation" in Project Settings and wrote this piece of code (it is more or less the unity doc sample)

private void Update()
{
    this.timer += Time.deltaTime;
    while (this.timer >= Time.fixedDeltaTime)
    {
        this.timer -= Time.fixedDeltaTime;

        controller.ApplyForces(controls);
        Physics.Simulate(Time.fixedDeltaTime);
    }
}

But when I play the game, movements are lagging a lot.
In fact, I can explain it: my game runs at 120 fps. The Time.fixedDeltaTime is set to 0.01667 (60fps). So 1 out of 2 frames are not simulating physics! In other words, half of my Update calls don't enter the while loop, which produce the jump effect.

I'm sure i'm missing something... what is the recommended way to do this? Should I call Physics.Simulate in FixedUpdate?

EDIT:

As someone asked me in the comments (then deleted the comment), I'm simulating Physics manually for networking reason. With manual physics, I can do predictions and corrections. Here's a link that describes the problem

EDIT2:

Thanks to @DMGregory and his excellent answer, I figured out that the problem comes from the lack of interpolation of my player rigidbody:
InterpolationMode seems to be handled by Unity when autosimulation is set to ON, which is not my case.

So now I'm looking how to handle this myself.

added 310 characters in body; edited tags
Source Link
Martin
  • 153
  • 1
  • 2
  • 9

I'm trying to simulate physics manualy in a Unity 3D game. I've turned off Physics "Auto Simulation" in Project Settings and wrote this piece of code (it is more or less the unity doc sample)

private void Update()
{
    this.timer += Time.deltaTime;
    while (this.timer >= Time.fixedDeltaTime)
    {
        this.timer -= Time.fixedDeltaTime;

        controller.ApplyForces(controls);
        Physics.Simulate(Time.fixedDeltaTime);
    }
}

But when I play the game, movements are lagging a lot.
In fact, I can explain it: my game runs at 120 fps. The Time.fixedDeltaTime is set to 0.01667 (60fps). So 1 out of 2 frames are not simulating physics! In other words, half of my Update calls don't enter the while loop, which produce the jump effect.

I'm sure i'm missing something... what is the recommended way to do this? Should I call Physics.Simulate in FixedUpdate?

EDIT:

As someone asked me in the comments (then deleted the comment), I'm simulating Physics manually for networking reason. With manual physics, I can do predictions and corrections. Here's a link that describes the problem

I'm trying to simulate physics manualy in a Unity 3D game. I've turned off Physics "Auto Simulation" in Project Settings and wrote this piece of code (it is more or less the unity doc sample)

private void Update()
{
    this.timer += Time.deltaTime;
    while (this.timer >= Time.fixedDeltaTime)
    {
        this.timer -= Time.fixedDeltaTime;

        controller.ApplyForces(controls);
        Physics.Simulate(Time.fixedDeltaTime);
    }
}

But when I play the game, movements are lagging a lot.
In fact, I can explain it: my game runs at 120 fps. The Time.fixedDeltaTime is set to 0.01667 (60fps). So 1 out of 2 frames are not simulating physics! In other words, half of my Update calls don't enter the while loop, which produce the jump effect.

I'm sure i'm missing something... what is the recommended way to do this? Should I call Physics.Simulate in FixedUpdate?

I'm trying to simulate physics manualy in a Unity 3D game. I've turned off Physics "Auto Simulation" in Project Settings and wrote this piece of code (it is more or less the unity doc sample)

private void Update()
{
    this.timer += Time.deltaTime;
    while (this.timer >= Time.fixedDeltaTime)
    {
        this.timer -= Time.fixedDeltaTime;

        controller.ApplyForces(controls);
        Physics.Simulate(Time.fixedDeltaTime);
    }
}

But when I play the game, movements are lagging a lot.
In fact, I can explain it: my game runs at 120 fps. The Time.fixedDeltaTime is set to 0.01667 (60fps). So 1 out of 2 frames are not simulating physics! In other words, half of my Update calls don't enter the while loop, which produce the jump effect.

I'm sure i'm missing something... what is the recommended way to do this? Should I call Physics.Simulate in FixedUpdate?

EDIT:

As someone asked me in the comments (then deleted the comment), I'm simulating Physics manually for networking reason. With manual physics, I can do predictions and corrections. Here's a link that describes the problem

Source Link
Martin
  • 153
  • 1
  • 2
  • 9

How to manually Simulate Physics properly on Unity?

I'm trying to simulate physics manualy in a Unity 3D game. I've turned off Physics "Auto Simulation" in Project Settings and wrote this piece of code (it is more or less the unity doc sample)

private void Update()
{
    this.timer += Time.deltaTime;
    while (this.timer >= Time.fixedDeltaTime)
    {
        this.timer -= Time.fixedDeltaTime;

        controller.ApplyForces(controls);
        Physics.Simulate(Time.fixedDeltaTime);
    }
}

But when I play the game, movements are lagging a lot.
In fact, I can explain it: my game runs at 120 fps. The Time.fixedDeltaTime is set to 0.01667 (60fps). So 1 out of 2 frames are not simulating physics! In other words, half of my Update calls don't enter the while loop, which produce the jump effect.

I'm sure i'm missing something... what is the recommended way to do this? Should I call Physics.Simulate in FixedUpdate?