- Home /
Change of rigidbody.velocity, Update or FixedUpdate?
Based on the manual, I understand that I should use FixedUpdate when "dealing with Rigidbody" object's component (ex. adding a force). Now... is assigning a value to it's velocity should be considered as "dealing with rigidbody"? To be more precise, I'm referring to the following piece of code, where should it end up?
speed = Mathf.Min(speed + 20 * Time.deltaTime, GetMaxSpeed());
rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, transform.up * speed, 0.03f);
Answer by Bunnybomb7670 · Mar 11, 2014 at 11:10 PM
well, update is called every frame, meaning depending on the framerate, it will be different, which may cause issues, if you're game is running at 100fps and you add 10 force, you will add 1000 force at 100fps but if it lags and goes down to 50fps, you will only add 500 force, which may not be what you want to do. fixedupdate however is not based the same way, the physics update rate is much more constant. I recommend that when editing ANY rigidbody stuff, always use fixedupdate to prevent framerate dependant issues occuring.
Yes that I actually know already. It seems that it all depends on what you're up to, but in most cases it seems that these types of code snippets ($$anonymous$$gling with rigidbody's) should end up in FixedUpdate.
Oh, also I found this link very useful in this case: http://docs.unity3d.com/Documentation/$$anonymous$$anual/ExecutionOrder.html
But does this also apply when setting the velocity directly? Because as far as I understand, this doesn't add up in each frame but applies the same consistent rate all the time.