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 /
This question was closed Jan 11, 2020 at 07:07 PM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
8
Question by DNP · Aug 04, 2012 at 08:42 PM · timereferencedeltatime

Time.deltaTime?

Can somebody explain to me a bit how Time.deltaTime works? Yes, i have seen the scripting reference page. But it doesnt really explain much. Im trying to learn, so can someone please help me out here?

Comment
Comments Locked
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

6 Replies

  • Sort: 
avatar image
27
Best Answer

Answer by aldonaletto · Aug 04, 2012 at 11:55 PM

Time.deltaTime is the time passed since last frame. If you want to move something at a constant velocity speed, for instance, multiply speed by Time.deltaTime and you will get exactly the distance moved by the object since last Update (remember: distance = velocity * time):

 var speed: float = 10;
 
 function Update(){
   // move the object forward at speed units per second:
   transform.Translate(transform.forward * speed * Time.deltaTime);
 }

You can use Input.GetAxis("Vertical") to control the movement:

 var speed: float = 10;
 
 function Update(){
   // move the object forward/backward at speed units per second:
   transform.Translate(transform.forward * speed * Input.GetAxis("Vertical") * Time.deltaTime);
 }

The same applies to the "Horizontal" axis, but not to "Mouse X", "Mouse Y" and "Mouse ScrollWheel"! That's because the axes Vertical and Horizontal return constant values in the range -1..1 when you press the buttons WSAD, while Mouse X, Y and ScrollWheel return the mouse/scrollwheel movement since last frame - thus these values already take the time into account.
Another use for Time.deltaTime is to decrement or increment time at constant rates. If you want to decrement a 10 seconds timer, for instance, you could do this:

 var timer: float = 10;
 
 function Update(){
   timer -= Time.deltaTime;
   if (timer <= 0) print("10 seconds ellapsed");
 }

You can use Time.deltaTime inside Update, coroutines or FixedUpdate (Unity returns the time since last FixedUpdate in this case), but you should not use it inside OnGUI because this function is called several times between frames.

Comment
Comments Locked · Show 5 · 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 DNP · Aug 05, 2012 at 12:33 AM 0
Share

Thank you for taking the time to explain this! :)

avatar image thesfid · Oct 10, 2013 at 03:40 PM 0
Share

It seems that the docs are misleading. To say "move the object forward at 10 units per second" using 10 * Time.deltaTime isn't really 10 units per second, it's 10 units per fraction of a second, right? $$anonymous$$aybe I'm misinterpreting...

avatar image Eric5h5 · Oct 10, 2013 at 03:56 PM 0
Share

What? No. "$$anonymous$$ove the object forward at 10 units per second" is exactly correct and not misleading at all. That's the whole point of Time.deltaTime; to make movement framerate-independent.

avatar image John-LZG · Mar 05, 2014 at 12:52 AM 3
Share

Imagine if a half second passed during an update, meaning time.deltaTime would return a .5.

speedPerSecond * time.deltaTime.

10 speed and .5 deltaTime means = 10 * .5.

5 "move" in that half second.

avatar image thesfid · Mar 05, 2014 at 02:53 PM 1
Share

Ah, I see now. Thanks for elaborating @John LZG.

avatar image
10

Answer by matriXcel · Jun 17, 2015 at 07:34 PM

Ok let me break this down to you like a boss say you are running the game at 100fps that would be 0.01secs/frame then deltaTime would be 0.01 and lets say you are moving a object 50m*Time.deltaTime on that one frame that would be 50m*0.01 it would move 0.5m/frame in proportion to 50m/sec ok now lets say you are still doing 50m*Time.deltaTime but on a slow computer that does 25fps that would be 0.25 secs/frame 50*0.25 would be 12.5/frame it is moving a greater distance because the frames are taking longer that is how Time.deltaTime compensates for hardware differentials. Time.deltaTime is lets say you have a countdown-=Time.deltaTime and the value of countdown is 10 it will subract 10 by 1 sec each sec but it is actually also subtracting it by the amount of time between frames if that makes sense that is why you will see floating point numbers.

Comment
Comments Locked · 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 PadraigCrowley · Aug 03, 2016 at 02:11 PM 2
Share

Your example is good but your maths are wrong for the 25fps example.

If the game is running at 25fps, it is running 0.04 sec/frame, not 0.25 This would mean it would move 2.0m/frame, (i.e. 50*.04m/frame), not 12.5

I'm not being pedantic, it's just that I'm a newbie and couldn't fully understand your example until I tried to work it out for myself.

Thanks.

avatar image anvitas · Jan 31, 2018 at 01:56 PM 2
Share

dude, I know you were trying to help.. but please use punctuation and commas..It's a struggle trying to understand your sentences

avatar image
7

Answer by drawcode · Aug 04, 2012 at 08:52 PM

If you want smooth animation across all hardware types you want to do things by time instead of by frame. By default Update is frame based. So you may have 30 frames a second or 60 frames a second or any variance from 0-hardware max.

Time.deltaTime allows you to move things not by frame (which would be too fast or too slow depending on your hardware). Time.deltaTime is essentially a fragment of a second or the time passed since the last frame. So if you move something with it in involved it will move them by time rather than by frame. Time.deltaTime is relative to the hardware you are on. If you aren't using it when you move things your game will play radically different on different hardware. Time.deltaTime returns a fraction of a second based on how long the last frame took for the hardware.

 The time in seconds it took to complete the last frame (Read Only).
 
 Use this function to make your game frame rate independent.
Comment
Comments Locked · 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 DNP · Aug 04, 2012 at 08:55 PM 0
Share

Can you give me an example on a way it could be used?

avatar image Eric5h5 · Aug 04, 2012 at 10:01 PM 0
Share

The docs provide a code example for how it's used.

avatar image
2

Answer by OfficialHermie · Feb 08, 2019 at 10:49 PM

Can we say that Time.deltaTime is a helper?

Let's say we want to move something. Now I write pseudo code...

 position += (1 meter * time.deltaTime;)

On my computer time.deltaTime is "1". Because it took 1 second to show the next frame.

This means:

1 meter * 1 = 1 meter. So time.deltaTime does not really do anything.

We have moved 1 meter in 1 second.

Now let's say we have a very slow computer.

You will notice that it's slow because time.deltaTime is "2" (not 1 like on my computer). It took 2 seconds until the next frame was shown.

Now what happens?

1 meter * 2 = 2 meters

We have moved 2 meters in 2 seconds.

That is still 1 meter per second.

So that's very good.

We still have the same result. :-)

The same applies to a very fast computer where time.deltaTime would be 0.001.

You will notice that you will always move 1 meter in 1 second if you use "* time.deltaTime".

Comment
Comments Locked · 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
0

Answer by jaldunate · Nov 23, 2016 at 04:28 PM

I remember once seeing a really old piece of software, running in a more modern computer, that was used to show an animation. In this more modern computer, the animation was running so fast that we could not understand how it was supposed to be.

Every computer has different processing power, and will render a different amount of frames per second. For example, if you change the position of an object one pixel per frame, the object will look like moving at different speeds depending on the FPS. Delta time makes this more consistent, because is a value taken from the system's clock. Multiplying the movement by the delta time instead of incrementing by an absolute value, will make the movement speed consistent across computers.

Comment
Comments Locked · 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
  • 1
  • 2
  • ›

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Multiple Cars not working 1 Answer

ERROR: It is not possible to invoke an expression of type 'float'. 1 Answer

Timer delay 1 Answer

Time.deltaTime not consistent over time 1 Answer

Time.deltatime is returning 0? 1 Answer


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