Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by mlovrekov · Oct 07, 2020 at 08:09 PM · fixedupdatedeltatime

Should I use deltaTime in FixedUpdate when setting velocity to a rigidbody?

I know there have been a lot of variations on this exact topic but I'm a bit confused from all the answers I've seen. So I'm moving a rigidbody by setting it's velocity in FixedUpdate by getting input (smoothed horizontal/vertical input, not raw) and multiplying it by the target speed. Relevant piece of code here

 Vector3 movementVector = Vector3.zero;
 Vector2 input = GetInput();                
 if (Mathf.Abs(input.x) > float.Epsilon || Mathf.Abs(input.y) > float.Epsilon)
 {
     // add speed to input vector
     movementVector = new Vector3(input.x * targetSpeed, 0f, input.y * targetSpeed); 
     // rotate the vector to where the character is facing
     movementVector = transform.localRotation * movementVector;
     // clamp magnitude to avoid faster speed on diagonals
     movementVector = Vector3.ClampMagnitude(movementVector, targetSpeed);
 
     body.velocity = new Vector3(movementVector.x, body.velocity.y, movementVector.z);
 }

So I am not changing the transform, I am setting the velocity and by all logic (in my mind) this should not be affected by framerate so in that regard I don't think I need to multiply by Time.deltaTime. However, I am fetching the smoothed input which does change between 0 and 1 in some amount of time so since that is a changing variable I feel like I might have to use dt here.

As you can see I'm quite confused with something that should be quite simple. What's the general consensus here? Do I have to multiply by dt when setting/changing velocity?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by unity_ek98vnTRplGj8Q · Oct 07, 2020 at 10:09 PM

I would say for 99% of projects you don't need to use a dt here. First let me explain why you might see a very small difference if you were using Update(), then I'll explain why you shouldn't see a difference in your project since you are using FixedUpdate()


To see what exactly the difference is take an extreme example. You have 2 versions of your game and you move your input from 0 to 1 over the course of 1 second. Your first version is running at 100fps and your second version is running at 1fps. In the first version, as you slowly move your input up your are constantly updating your velocity to match, resulting in a smooth transition. In the second version your velocity only updates once during that time, going from 0% to 100% in one frame. In both versions you will hit the same velocity in the same amount of time, but in the second version you are losing all the distance that your object would travel during that acceleration phase between your inputs of 0 and 1. While this would be a lot at drastically different framerates or for drastically high velocity changes, I doubt it would be noticeable in most games.


Now consider that you are using fixed update. Fixed update loop doesn't change based on computation load like normal update does. Because fixed update has a constant update period, multiplying by FixedDeltaTime is the same thing as simply multiplying by a constant. The ONLY case where you could experience any kind of problems is if you are changing your fixed update loop dynamically (which I definitely don't recommend).


Hope this answer helped

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mlovrekov · Oct 10, 2020 at 03:07 PM 0
Share

Wow this is really informative. Thank you for taking the time to explain all this. I hope you don't $$anonymous$$d but I got a few more questions.

... but in the second version you are losing all the distance that your object would travel during that acceleration phase between your inputs of 0 and 1. While this would be a lot at drastically different framerates or for drastically high velocity changes, I doubt it would be noticeable in most games.

So if I want to make absolutely sure that this discrepancy is not noticable no matter the framerate or magnitude of change in velocity i should multiply by dt?

Also one thing I'm a bit confused by is: Let's say that the target speed is 16. If I multiply that by dt then that means that the entity will move 16 pixels per second while otherwise it will move 16 pixels per frame? Am I right in that assumption?

avatar image unity_ek98vnTRplGj8Q mlovrekov · Oct 12, 2020 at 07:02 PM 0
Share

Since you are using fixed update there will be no discrepancies due to variable frame rate because the fixed update frame does not ever change unless you change it manually. So no, do not multiply by dt. If you were using Update instead of fixed Update you could multiply by dt, but not directly because just multiplying your velocity by dt will make you move slower. You would have to change the way you were applying velocity, and it wouldn't work perfectly either way. TLDR: don't worry about it in this case.


Because you are setting velocity you should not multiply by dt directly ever. If, however, you were moving an object by incrementing its position every frame, then you should use dt.

 public float speed = 10f;
 
 void Update(){
 
     //Approach #1
     rigidbody.velocity = speed; //Do not multiply by dt here. You are setting the velocity which is a constant value
     //In other words the velocity at any moment in time does not depend on the time since the last frame.
 
     //Approach #2
     transform.position += speed * transform.forward * Time.deltaTime;
     //Here you do multiply by dt because the amount you should move every frame depends on how long it has been since the last frame
 }

Here a speed of 10 means that you want the object to move 10 units every frame (not pixels, which depend on the location and size of the camera). If you are setting velocity then that's that, but if you are moving the object's position directly then remember Velocity = Distance over Time, or in other words Distance equals Velocity times Time, therefore if you want to move an object at a certain velocity by changing its position, you need to move it by Vecocity x Time every frame, so in this case speed * Time.deltaTime

avatar image
0

Answer by SunnyChow · Oct 08, 2020 at 03:20 AM

use Time.fixedDeltaTime for FixedUpdate, and Time.deltaTime for Update.

I would say it's much easier for long term development if your script multiply delta time. 1, the speed value will make more sense. it's per second instead of per frame. Every unity API value is per second so it's better to stick with it so you won't confuse yourself in the future. 2, you are assuming the frame rate is always the same, but there may be lag, or your game need to support weak device that run it in lower frame rate.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mlovrekov · Oct 10, 2020 at 01:50 AM 0
Share

the speed value will make more sense. it's per second instead of per frame.

The speed was 16 before. To get that same movement speed after multiplying with dt I had to set it to 800. What is 800 in this case? (Units?)Pixels per second? Also Unity docs say that using Time.deltaTimein FixedUpdate is the same as using Time.fixedDeltaTime
avatar image Bunny83 · Oct 10, 2020 at 05:09 PM 1
Share

This is wrong on multiple levels. First of all Time.deltaTime will return the value of Time.fixedDeltaTime when used from inside FixedUpdate. You never need or should really read fixedDeltaTime. This makes code much more flexible in case it gets moved from FixedUpdate to Update. Time.fixedDeltaTime is mainly used for setting the fixed update rate.


Though the biggest issue with this answer is to suggest to multiply be deltaTime in this case. He is setting the velocity to a certain value. This is not a process that happens over time. The velocity of an object is already given in units per second. The physics system uses it to move the object every physics step where it does apply delta time automatically.


Lastly FixedUpdate runs at a "fixed" call count per second. This call count is ensured. Even when your framerate is just 10 fps, FixedUpdate will still be called 50 times per second. In this case it will be called about 5 times per frame. If the frame rate is 100 fps it will only be called every second frame. That's how fixed update works. I once made this little helper which allows you to implement your own / seperate fixed update callbacks that run at whatever framerate you want. That's essentially how Unity's fixed update works internally. Even longer ago I made this visualization to give a better understanding what FixedUpdate actually does.

avatar image mlovrekov Bunny83 · Oct 11, 2020 at 04:12 PM 0
Share

Thanks for the explanation. Just a quick question. Is the fact, that I'm using non-raw input and multiplying that input value by speed, messing anything up? Will the rigidbody accelerate differently on different FPS's and if yes what should I do to fix that?

avatar image
0

Answer by tutaBugarin · Oct 07, 2020 at 08:31 PM

For me, only FixedUpdate runs smoothly and lag doesn't make problems.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting fixeddeltatime depending on TimeScale makes physics behave weirdly 1 Answer

Lerp stopping/not working? 1 Answer

Camera Stutter on RayCast Collision Problem 0 Answers

Time effected Bullets 0 Answers

Odd character dash discrepancies. Time.deltaTime? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges