最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Will this object be faster on a higher framerate, since it's in Update()? - Stack Overflow

programmeradmin1浏览0评论

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
  • 1 Quote from the docs "In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour - use AddForce instead Do not set the linear velocity of an object every physics step, this will lead to unrealistic physics simulation. A typical usage is where you would change the velocity is when jumping in a first person shooter, because you want an immediate change in velocity." – BugFinder Commented Feb 4 at 16:11
  • 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 through FixedUpdate .. but besides that time-based values are just fine in both as long as you use them correctly .. in your case the linearVelocity already is a value in units per second so this already is frame rate independent – derHugo Commented Feb 4 at 17:36
  • 1 @MakePeaceGreatAgain this question isn't about performance at all but rather whether/how the movement in 3D space will be influenced by the frame rate – derHugo Commented Feb 4 at 22:56
Add a comment  | 

2 Answers 2

Reset to default 2

I 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;
发布评论

评论列表(0)

  1. 暂无评论