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
5
Question by xikky · Jul 10, 2013 at 10:07 PM · timedeltatimesmoothdamp

When to use Time.deltaTime?

I have an idea of what Time.deltaTime is and what it is used for, but I'm not sure if I need to use it in my case. I am moving the position of Transfrom using Vector3.SmoothDamp:

 transform.position = Vector3.SmoothDamp(transform.position, targetPosition, velocity, 0.2);

targetPosition is a Vector 3 value for touch position on the screen. Thus the object is following the touch position. Should I multiply values of x, y and z by Time.deltaTime for targetPosition?

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
10
Best Answer

Answer by Eric5h5 · Jul 10, 2013 at 10:34 PM

You must always use Time.deltaTime when moving objects over time, either by supplying it yourself or by making use of functions like SmoothDamp that use Time.deltaTime by default (hardly any functions do that though). But you must never use Time.deltaTime for input that's already framerate-independent, such as mouse movement, since in that case using Time.deltaTime will do the opposite of what you intend.

If you're ever unsure you can always toggle vsync on and off and observe any potential speed differences. If the game's already running close to the monitor refresh rate anyway, then make use of Application.targetFrameRate instead.

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 xikky · Jul 10, 2013 at 10:38 PM 0
Share

Perfect! Thanks :)

avatar image SarperS · Dec 07, 2014 at 06:23 AM 0
Share

I didn't know the input value's were frame rate independent by default. Was trying to fix a problem due to Input.mouseScrollDelta being so. Is it written anywhere on the official docs?

avatar image Eric5h5 · Dec 07, 2014 at 06:32 AM 1
Share

Doesn't need to be written anywhere, just think about it for a second: if you move the mouse, say, 3 inches over the course of 1 second, the distance you moved the mouse and the time it took was completely unrelated to the framerate of your computer. The numbers you get from the mouse over the course of 1 second will add up to 3 inches regardless of whether you get 10fps or 500fps.

avatar image
25

Answer by DavidDebnar · Jul 10, 2013 at 10:38 PM

Time.deltaTime should be used, when you want movement or any other constant variable change, to be the same speed, no matter the framerate.

How does it work?

DeltaTime is the time between the last and current frame. If the game's running at 10 fps, it's 0.1 (1 / fps), if the game's running at 20 fps, it's 0.05.

How does this help?

Lets take:

 transform.position += Vector3.right * 5;

This addition doesn't have deltaTime in it. This means, if the game's at 30 fps, it'll move 150 world units a second, but if the fps is 60, it'll be 300 world units.

The second example includes deltaTime:

 transform.position += Vector3.right * 5 * Time.deltaTime;

In this case, if the game's fps is 30, it'll add 5 0.033 each frame, resulting in 5 units/second. The second case would be 60 fps, so the code would add 5 0.0166 units per frame to the x position, with the same result, 5 units/second.

All this can be turned into a general equation:

When:

 x += speed * (1 / fps) = speed / fps

Then, to get the result for one second, we need to multiply the right side by fps, which results in:

 x[metres] = speed[m/s] * t[s];

--David--

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 xikky · Jul 11, 2013 at 06:45 AM 0
Share

Thanks, your explanation also helped to understand deltaTime more

avatar image cryptex. · Dec 24, 2015 at 10:31 PM 0
Share

Thank you so much !! Great explanation. I finally understood it.

avatar image mani3307 · Dec 02, 2017 at 02:19 PM 0
Share

can you explain the calculations done for

transform.position.x += 5 * Time.deltaTime;

clearly

avatar image
2

Answer by DocteurCox · Jul 10, 2013 at 10:12 PM

You should use Time.deltaTime for any framerate dependant thing. If you were just setting your object position to touch position, then there would be no point using Time.deltaTime since no matter the framerate, your object would still be at touch pos. But since your trying to smoothly go to that position, then yes : you should use Time.deltaTime to make sure your object moves equally fast whether your game is running slow or fast. ;)

There is no need to pass deltaTime to that function though, it already has deltaTime passed by default !

Hope it helps !

Comment
Add comment · Show 6 · 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 xikky · Jul 10, 2013 at 10:16 PM 0
Share

Thanks. Sometimes I have difficulty trying to deter$$anonymous$$e which is framerate dependent and which is not. How would I fix my movement then? Should I multiply x, y and z by Time.deltaTime for targetPosition?

avatar image DocteurCox · Jul 10, 2013 at 10:22 PM 0
Share

I don't think that's necessary since SmoothDamp does it by himself, but could you describe your problem more precisely ?

avatar image xikky · Jul 10, 2013 at 10:25 PM 0
Share

The transform is an is$$anonymous$$inematic rigidbody, which is jointed to a non $$anonymous$$inematic rigidbody. What is happening is that the transform is dragged smoothly towards the finger and thus the other rigidbody follows due to the joint. For the smooth damp, currentPosition is the position of the is$$anonymous$$inematic rigidbody, and target is the finger position.

avatar image xikky · Jul 10, 2013 at 10:27 PM 0
Share

I want the drag movement to be consistent on all devices. Currently I cannot test this as I don't have any other device.

avatar image DocteurCox · Jul 10, 2013 at 10:31 PM 0
Share

I understand what you are trying to achieve, however I still don't understand the problem. What's wrong with your movement ?

Show more comments

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

22 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

Related Questions

Force mode and delta timing 1 Answer

Different behavior between editor and standalone changing time step 0 Answers

High Speed 1 Answer

Time.timeScale and Time.fixedDeltaTime 1 Answer

Time.delta Time inconsistant 5 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