Hi everyone, I’m pretty new to Unity and I’m quite confused about what a good practice using Update and FixedUpdate.
Pls correct me if I’m wrong, I’ve learned somewhere that you should put your “GetInput” codes in Update, but when you want to do something related to physics, like Rigidbody, you should use FixedUpdate. Let’s say I have a character and a Move method, which contains both “GetInput” and “Physics” codes, where do I put it in? Or should I not have that method at all and just split those codes into Update and FixedUpdate respectively? If that’s the case, it’s quite unconvenient when I need to use the Move method in another class.
It depends a bit on exactly what kind of physics forces you’re using. If you’re applying forces on every (or nearly) every frame, it’s better to do that in FixedUpdate. For example, if you have a rocket that flies upward, you’d probably want to be applying that force in FixedUpdate.
On the other hand, if you’re apply more or less “one time” forces, it’s fine to put that in Update. For example, if you’re making the character jump on when Space is pressed, you’d apply an upward force just on the frame when the jump occurred. That’s fine to keep in the Update method.
I use FixedUpdate essentially to apply constant forces, and everything else goes in Update.
One-time input should always be in Update(). If needed, use a variable to pass it to FixedUpdate(). Like ‘KeyDown’, for instance, as this could be missed in the FixedUpdate() loop. Continuous input , such as “GetKey”, can be safely used in FixedUpdate, in my opinion & experience
Sure, you might miss the down, but essentially you get them when needed.
Someone responded as I was typing, and I agree about one-time forces vs. continuous, also.
Hope that helps 
Thank you guys. My character is supposed to move while a key is being held down. So I assume that it’s completely fine to put my Move method in FixedUpdate?
the problem with assuming that you can just use the input in fixedupdate is that the input can feel “laggy”, think about the timing of the input: the user presses or releases the key just after a physic tick, for the next fraction of a second nothing happens as FixedUpdate hasn’t run. It might not be huge, but it can be something that just makes games feel awkward to play.
Much better to take the input values in Update and pass them into the FixedUpdate calls.
Thanks, that helps me a lot :))
Use interpolation on rigidbodies if they’re not smooth using this method.
I have never notice in my testing any noticeable delay while holding down a key for input in FixedUpdate, just for the record** 