I'm in Unity 2D using C#.
So, I know that normally putting a value that is time-based into Update() is normally bad, but will the same happen for a Rigidbody2D? I'm not sure, but it seems to calculate the rate itself...
Here is the code:
void Update()
{
rb.linearVelocity = transform.right * velocity * 3f;
}
Will its speed differ, based on the framerate?
I'm in Unity 2D using C#.
So, I know that normally putting a value that is time-based into Update() is normally bad, but will the same happen for a Rigidbody2D? I'm not sure, but it seems to calculate the rate itself...
Here is the code:
void Update()
{
rb.linearVelocity = transform.right * velocity * 3f;
}
Will its speed differ, based on the framerate?
Share Improve this question asked Feb 4 at 16:02 Duck0Duck0 192 bronze badges 3 |2 Answers
Reset to default 2I wont differ because your code just changes the velocity of the gameobject. If velocity changes, speed changes but since transform.right * velocity * 3f
is always same, velocity doesnt change, so speed doesnt change.
You can try this yourself by going to game window, selecting resolution option then turning Vsync on or off. Enabling Vsync will lock your fps to 60, disabling it will increase your fps.
You can see your fps from going to game window and clicking stats option
Use Update() with Time.deltaTime. This is the interval in seconds from the last frame to the current one. Without Time.deltaTime, the speed might be faster when the fps is higher.
rb.linearVelocity = Time.deltaTime * transform.right * velocity * 3f;
putting a value that is time-based into Update() is normally bad
.. who told you that? In general when using physics it is recommended to rather go throughFixedUpdate
.. but besides that time-based values are just fine in both as long as you use them correctly .. in your case thelinearVelocity
already is a value inunits per second
so this already is frame rate independent – derHugo Commented Feb 4 at 17:36