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
0
Question by Gizmoa · Apr 18, 2011 at 04:46 PM · updatetimedeltatime

Time.deltaTime not working correctly

Something very strange appears to be happening with Time.deltaTime when used in the Update loop in C# Scripts. Time.fixedDeltaTime does not have this problem, and I have not tested it in Javascript.

Essentially, it appears that Time.deltaTime is not correctly calculating or that Update is not being called consistently with each frame because when used in any form (including it's simplest) such as:

using UnityEngine; using System.Collections;

public class example : MonoBehaviour { void Update() { float translation = Time.deltaTime * 10; transform.Translate(0, 0, translation); } }

the script results in inconsistent movement of objects. It was a fellow team member who first noted the bug when a few bullets which had been slowed down for testing started to move more quickly when unrelated character controllers received input from an x-box controller.
I soon discovered that although it is most obvious in this context, it appears to be distorting ALL uses of Time.deltaTime. While I was able to remove it from most of the code and convert most movement systems to be based on physics, there are many other places it is used where timing is vital.

Does anyone know what could be causing this problem, or if it is a bug within Unity itself, and what, if anything, could be done in it's place/to fix it?

My biggest concern is that the character controller Move function normally takes in a value multiplied by Time.deltaTime and I can see this becoming a major problem.

Thanks.

Comment
Add comment · Show 5
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 Owen-Reynolds · Apr 18, 2011 at 11:06 PM 0
Share

For fun, I'd make public float T=0; and jam T+=Time.deltaTime; into update. $$anonymous$$aybe do the same thing with fixedTime or just Time.time as a control. Check if it really is moving wrong.

avatar image AngryOldMan · Apr 19, 2011 at 02:51 AM 0
Share

check updated answer :)

avatar image Peter G · Apr 19, 2011 at 03:00 AM 0
Share

Do you have a s$$anonymous$$dy frame rate? Since Time.deltaTime measure the frame before, I could see that if your FPS is highly inconsistent that you could possibly have some lag issues-this is theoretical though, I've never had any problems with Time.deltaTime.

avatar image Gizmoa · Apr 20, 2011 at 04:41 PM 0
Share

This could explain it Peter. Thank you for clarifying how the fps is calculated.

avatar image flim · Apr 05, 2012 at 06:04 AM 0
Share

Same here, I wonder why no one found this problem. If I manipulate the object position with Time.deltaTime, the movement is not always consistent, the movement could be faster or slower in Unity Editor, depends on what you selected in Hierarchy tab, and standalone full screen seems slower than webplayer, pretty weird.

This problem not so apparent maybe most Unity games only use the physics engine.

7 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Eric5h5 · Apr 19, 2011 at 03:25 AM

Time.deltaTime is simply the time since the last frame, so there's not really anything that could go wrong with it. It always works, or at least it's never not worked in the 4 years I've been using Unity. The example code you posted is fine, so you have issues elsewhere. You could be changing Time.timeScale or something.

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 Gizmoa · Apr 20, 2011 at 04:41 PM 0
Share

I spoke with my father (he has been coding since punch cards) and apparently it is possible to get time drift with frame checks because occasionally the call to the clock will fail when too many things attempt to check it at the same time. Without knowing exactly how Unity finds Time.deltaTime it is hard to know exactly what is happening. Regardless, the drift is very small and really can only be seen when I slow everything down, so I'm not going to worry about it. I would like to learn more about how Unity calculates the value, but I will live without it xD. Cheers!

avatar image Thewhiteaura · Jul 01, 2015 at 08:05 AM 0
Share

You sir, are my hero!!! You have solved something that has been perplexing me for a few hours now, I forgot that my pause menu simply used time.timeScale, and there's a quit level button there... making me scratch my head and wonder why my timer stopped counting before it called for a timecheck, making it impossible to regain any energy xD! Thank you so much for saving me a few more hairs ^^,

avatar image
1

Answer by AngryOldMan · Apr 18, 2011 at 04:58 PM

change Time.smoothDeltaTime to get a less jerky reaction. Time.deltaTime is the time between frames so it's always going to be inconsistent even sometimes when not moving.

EDIT

Ah i see I must have misunderstood, my bad. Instead of using tranform.translate use teansform.position and it will appear to be jerky rather than smoothly moving

Comment
Add comment · Show 1 · 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 Gizmoa · Apr 18, 2011 at 06:59 PM 0
Share

Yes, it should be inconsistent to correctly calculate the amount of time passed,but that isn't what my problem is.

The problem is it is not calculating this correctly, resulting in inconsistent movement per second.

The entire point of Time.deltaTime is to make movement and other calculations work independently of frame rate, but frame rate continues to affect my calculations (or something is). In other words... I "WANT" it to be jerky and it's not happening lol if that makes sense.

If anything it's as though it is over compensating for the frame rate.

avatar image
1

Answer by Joe 24 · May 18, 2011 at 05:05 AM

Hi everyone Did you get a solution to this? I'm basically calculating the position of a character around a circumference by adding to the angle. I want to multiply the angle by deltaTime so it's consistent with the frame rate, but when I do, the animation starts jerking out, basically the character will move forward in the path but it would irregularly jump back and forward a pixel or two. This is the code I'm using:

angle += increment * Time.deltaTime; angle = angle - (Mathf.Floor(angle/360))*360; Debug.Log(angle); float nZ = Mathf.Sin( angle*Mathf.Deg2Rad) * radious; float nX = Mathf.Cos(Mathf.Deg2Rad*angle) * radious; movement = new Vector3(nX-character.transform.position.x, 0, nZ-character.transform.position.z);

     character.Move(movement);

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
avatar image
0

Answer by Gizmoa · Apr 20, 2011 at 04:44 PM

Thanks everyone for your help with this question and the clarifications on how Unity calculates Time.deltaTime.

Comment
Add comment · Show 1 · 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 Eric5h5 · Apr 20, 2011 at 05:09 PM 0
Share

Please don't post comments as answers.

avatar image
0

Answer by flim · Apr 05, 2012 at 03:38 AM

The Time.deltaTime return different result if I didn't select anything in Hierarchy tab, or if the "Maximize on Play" is on.

Also the publish target has different result, the rotation works for web player, but not work in standalone full screen.

Very strange :-|

I found the following code would have affect by the Unity editor:

The value movementX is higher when there are something selected in Hierarchy tab, and smallest when run in "Maximize on Play" is selected.

Code:

 void Update() {
         mouseX = Input.mousePosition.x;
         mouseY = Input.mousePosition.y;
  
         cameraScreenXY = Camera.main.WorldToScreenPoint (transform.position);
        
         deltaX = mouseX - cameraScreenXY.x;
         deltaY = mouseY - cameraScreenXY.y;
        
         oldX = transform.localPosition.x;
         oldY = transform.localPosition.y;
  
         transform.localPosition = new Vector3 (
             transform.localPosition.x + (deltaX * Time.deltaTime * moveSpeed / screenWidth),
             transform.localPosition.y + (deltaY * Time.deltaTime * moveSpeed / screenHeight),
             transform.localPosition.z);
        
         movementX = (transform.localPosition.x - oldX);
         movementY = (transform.localPosition.y - oldY);
 }

I found that the Time.deltaTime cause the problem, for instance, keep the the Game window resolution unchange, if I select and unselect object in Hierarchy tab I will get different maximum movementX value.

Is the Time.deltaTime has bug?

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
  • 1
  • 2
  • ›

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

2 People are following this question.

avatar image avatar image

Related Questions

Execute code every x seconds with Update() 4 Answers

Time.deltaTime not consistent over time 1 Answer

Single Step (pause, resume), or externally clock game loop 0 Answers

Add 1 Per Second to an Int 1 Answer

Movement using Time.deltaTime not working on Fast mac 3 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