Physics after build feel different vs In-Editor (Time.deltaTime)
Asked Answered
B

2

0

Hey all so I ran into this issue here.

Here’s a quick video to summarize it:

Let me know if you need any more details.

Thanks for your help in advance!

Bighead answered 15/3 at 17:52 Comment(2)

@AlwaysSunny Hey could you help me with this? ^ ~The guy you helped make the third person camera a while ago :)

Bighead

If this is using physics, you shouldn't really be using Update() as the physics don't update themselves every frame. Have you tried porting the physics to FixedUpdate() with time.fixedDeltaTime instead?

Fermentative
B
0

@Fermentative @Erythrite

I’d like to thank both of you so much for all your help! I finally fixed it!

For anyone else suffering from these issues,

Use FixedUpdate() and Time.fixedDeltaTime for physics and movement!

These changed EVERYTHING!

I got 144fps both in the editor and on a standalone build.
The actual gameplay is now 1:1.

Originally, I didn’t want to use FixedUpdate() that much because it looked like all the movement was jittery. Although I think Time.fixedDeltaTime was the trick to fix that.

I did tweak some project settings but to no avail. It was FixedUpdate() and Time.fixedDeltaTime that fixed everything!

Thanks for your help!

Bighead answered 6/6, 2023 at 2:35 Comment(2)

Aha! And that's why it's always a good idea to show your code. ;) You have stumbled upon the correct paradigm for managing physics-related activities: Get your input in Update and drive anything physics-related in FixedUpdate. Still not sure why it impacted framerate in the way it did, but I'm glad you got it sorted out!

Erythrite

No worries and I'm glad that video helped you!

Peek
T
0

Hey y’all, I seem to be struggling with the same thing. I have a spaceship flight sim going on right now, and here’s the code to move:

void Thrust() {
    if (Input.GetAxis("Accelerate") > 0 && !atWall)
    {
        transform.position += transform.forward * Speed * Time.fixedDeltaTime * Input.GetAxis("Accelerate");
    }
    else
    {
        Speed = baseSpeed;
        isBoosting = false;
    }

    if (Input.GetButton ("Turbo") && canBoost && !atWall) {
		Speed = boostSpeed;
		isBoosting = true;
		Debug.Log ("pressing space");
	} else {
		//turn off boost when not pressing the button
		Speed = baseSpeed;
		isBoosting = false;
	}
}

So not only is the physics being handled under fixedUpdate, but the if statements handling the input as well. It fixed the timing issue in the editor when I hit the play on the spaceship scene, and when I start on the main menu and transition to the ship scene.

And here’s the code for using the boosters:

if (isBoosting) {
	//use boost
	Stamina -= staminaMultiplier;
} else if (!isBoosting && Stamina < maxStamina && canRegen) {
	//recover boost. canBoost basically caps so you cant go over 100 when you're resting
	Stamina += staminaMultiplier;
} else {
	
} 

But yet when I compile/build the game, I noticed that the stamina/fuel for using the boosters goees down/fills up around 10x slower than it does in the editor? Despite having fixed the continuity error between scenes in the editor already. I’m still confused as to why FixedUpdate and fixedDeltaTime don’t seem to be working when compiled into the exe

Tuinenga answered 15/3 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.