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 Linden Holt · May 11, 2013 at 01:59 PM · optimizationspeedframeratetime.deltatime

I do not understand Time.deltaTime

This would be my 1st question on Unity Answers, I'm usually able to find something relevant by googling. I'm making a standalone physics based first person puzzle platformer as a stand alone on PC. My problem is that when my framerate is much higher in the 'fastest' version of the build then everything speeds up to the point where the Benny Hill theme would not be ill suited.

Not only is it scripts I've made, but the speed of the '1st person controller' from the 'standard assets' is significantly increased. I then used 'Application.targetFrameRate' to see what happens when the framerate is decreased and sure enough everything slows down significantly.

I mentioned the controller, but there's also this rotation script:

 function update(){
 
 if (NS) { transform.Rotate(Vector3(x, y, z)* Time.deltaTime); }
 
 if (HS) { transform.Rotate(Vector3(xSped, ySped, zSped)* Time.deltaTime); } 
 }
 

and a camera fade script:

     function OnGUI(){
  
     alpha += (fadeDir * fadeSpeed) * Time.deltaTime;    
     alpha = Mathf.Clamp01(alpha);    
  
     GUI.color.a = alpha;
  
     GUI.depth = drawDepth;
  
     GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
 }

I'm more than a bit confused, Time.deltaTime doesn't seem to be helping. Would it be safe to just set a target framerate and hope that the game runs close enough to it that it doesn't feel too strange?

Any advice?

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

1 Reply

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

Answer by aldonaletto · May 11, 2013 at 03:07 PM

Time.deltaTime has no meaning inside OnGUI, because this function is called multiple times each frame. You should move the alpha calculation to Update and use the alpha value inside OnGUI, like this:

 private var alpha: float;
 
 function Update(){
     alpha += (fadeDir * fadeSpeed) * Time.deltaTime;   
     alpha = Mathf.Clamp01(alpha);  
 }
 
 function OnGUI(){
     GUI.color.a = alpha;
     GUI.depth = drawDepth;
     GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
 }

Outside FixedUpdate, Time.deltaTime is the time elapsed since last frame. Inside FixedUpdate, it's a fixed value (20mS, by default). Multiplying the desired angle by Time.deltaTime makes it frame rate independent - for instance:

  transform.Rotate(Vector3(0,90,0) * Time.deltaTime);

This code should rotate the object at 90 degrees per second about the Y axis.

The First Person Controller moves at constant speed, independent of the frame rate. If it's moving too fast, something is very wrong in your project - create a new project and verify if things work as expected in it.

Comment
Add comment · Show 4 · 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 Linden Holt · May 11, 2013 at 03:14 PM 0
Share

Thanks for such an insightful response! So the rotation script should be in Update and not FixedUpdate, correct?

And yes, I feel like the me from 6 months ago must have done something to the project settings to create these problems.

Thanks again for taking the time to help.

avatar image aldonaletto · May 11, 2013 at 03:35 PM 0
Share

It's better to keep the rotation code inside Update: at low frame rates, FixedUpdate is called multiple times between frames; at high frame rates, several frames may be rendered between FixedUpdate calls. This may result in choppy movement or rotation, since the amount moved or rotated each frame isn't precisely related to the last frame duration.

avatar image Fattie · May 11, 2013 at 04:00 PM 0
Share

Ah of course it was in OnGui !

I didn't even noticed ! Thank goodness for Aldo.

avatar image Fattie · May 12, 2013 at 06:57 AM 0
Share

Again a great call by Aldo "keen eyes" Naletto !! :)

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

16 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

Related Questions

FixedUpdate and Time.deltaTime slowe 2 Answers

Locate cause of massive frame rate drop 0 Answers

Performance of game depends on computer's power option? 0 Answers

Animation of terrain causes drastic performance drop (fps) 0 Answers

Using time.deltatime to move from coordinate to coordinate is too fast? 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