Update or FixedUpdate?

Hi !

To cut a long story short, I have a very simple code, for now, which makes the rigidbody velocity equals a new vector3 consisting of Input.GetAxes multiplied by speed.

Should I put this code in Update multiplying movement by Time.deltaTime or in FixedUpdate?

Should I handle the GetAxes in Update then cast it to a variable and use it in FixedUpdate ?

Thanks in Advance

If you are doing any calculations with the rigidbody, put it in the FixedUpdate. If you are just assigning values to properties you can really put that where ever…

1 Like

Input in Update. Physics in FixedUpdate.

2 Likes

I read before that Fixed updates in general are called only once per frame. (minimum)

No. Update is called once per frame.

FixedUpdate can be called 0, 1, or many times per frame.

2 Likes

That makes sense, I now understand it depends on what the developer set the update rate as.

1 Like

Actually, that’s what everyone says, but I want to know what to do in my case.

Should I put the whole code in FixedUpdate?
Should I make variables and assign these variables in Update to the Axes?

Can I then put my whole game code in FixedUpdate?

the problem with putting input code into FixedUpdate is that because fixedupdate isn’t guaranteed to be called every frame your game might miss the input.

This is hugely simplified but it’ll give you the sense of the issues you might face:
Let says you’re sticking with the default 50 frames a second for the physics tick, but you’re game isn’t too taxing and update is running at 100 frames per second.

Now you tap the space bar to “jump” (or whatever) such that the input only lasts for 1 frame. If you have the input code in the fixed update you’ve got a 50/50 chance the input code is caught on the same frame as a physics tick and processed.

What you “should” do is highly dependant on what you are trying to do. If you’re writing a fast paced fighting game, you’ll notice the problems. If you’re writing something a lot slower, you might not. :eyes:

If you’re setting the velocity based on continuous input (controller stick/keyboard button/mouse held down), you can set it in Update and call it a day. That would be appropriate for something like a shmup.

If you’re reading input events, and applying force a single time, as you’d do if a single key press caused a jump, you’ll want to split it up - do input in Update and apply the input in FixedUpdate.

1 Like