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 DangerPenguin · Jun 19, 2014 at 03:38 AM · javascriptdeltatimedelta

Time.deltatime multiplied by variable is different than by a number literal

After being away from unity for a while I've started a new project, and I've working on something that needs Time.deltatime, but I'm getting weird results.

Multiplying Time.deltatime by a number literal yields a different result that multiplying Time.deltatime by a variable with the same value. It's off by a factor of 1000... I can't tell if I'm overlooking something in UnityScript that's causing this to occur.

I'm using Unity ver 4.5.0f6 on Mavericks, scripting in UnityScript. Here's a stripped down example and what I'm getting as output.

 #pragma strict
 
 var fade_out_speed : float = 5.0f;
 
 function Update () {
     Debug.Log("==========");
     Debug.Log(Time.deltaTime);
     Debug.Log(Time.deltaTime * 5.0f);
     Debug.Log(Time.deltaTime * fade_out_speed);
     Debug.Log(Time.deltaTime * fade_out_speed * 1000);
 }

Gives me the output

==========

UnityEngine.Debug:Log(Object)

0.01775795

UnityEngine.Debug:Log(Object)

0.08878976

UnityEngine.Debug:Log(Object)

8.878976E-05

UnityEngine.Debug:Log(Object)

0.08878976

UnityEngine.Debug:Log(Object)

Comment
Add comment · Show 2
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 tswalk · Jun 19, 2014 at 04:26 AM 0
Share

interesting... after specifying it to be public it didn't affect the calculation in C#... definite difference between float and double though :)

avatar image haico1992 · Jun 19, 2014 at 06:53 AM 0
Share

seem like the output converted to second if datatype is float ins$$anonymous$$d of "milisecond". Could be Unity framework bug?

Try print that value to a UI element ins$$anonymous$$d of the console, avoid that Debug.Log function.

2 Replies

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

Answer by robertbu · Jun 19, 2014 at 03:40 AM

Since fade_out_speed is public, check in the Inspector to make sure the value is 5.0. Or alternately, make fade_out_speed private and rerun the test.

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 DangerPenguin · Jun 19, 2014 at 04:15 AM 0
Share

Wow, that was exactly it. I hadn't touched it in the inspector yet, but making it private did the trick!

avatar image
0

Answer by tswalk · Jun 19, 2014 at 04:13 AM

perhaps it's an issue with their java precision... no idea, but when I cast it as a float in C# there's a bit of rounding happening on the operations. Casting as doubles is interesting though :)

 public class dtimer : MonoBehaviour
 {
     private bool _executed;
 
     void OnEnable()
     {
         _executed = false;
     }
     void Update()
     {
         double fade_out_speed = 5.0d;
         double dtime = Time.deltaTime;
         double _calculated;
         string _log;
 
         if (!_executed)
         {
             Debug.Log("==========");
             _calculated = dtime;
             _log = string.Format("deltaTime: {0:N9}", _calculated);
             Debug.Log(_log);
 
             _calculated = dtime * 5.0d;
             _log = string.Format("deltaTime * 5.0d: {0:N9}", _calculated);
             Debug.Log(_log);
 
             _calculated = dtime * fade_out_speed;
             _log = string.Format("deltaTime * fade_out_speed: {0:N9}", _calculated);
             Debug.Log(_log);
 
             _calculated = dtime * fade_out_speed * 1000.0d;
             _log = string.Format("deltaTime * fade_out_speed * 1000.0d: {0:N9}", _calculated);
             Debug.Log(_log);
             _executed = true;
         }
     }
 }


results:

deltaTime: 0.020000000

deltaTime * 5.0d: 0.099999998

deltaTime * fade_out_speed: 0.099999998

deltaTime fade_out_speed 1000.0d: 99.999997765

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Cable-building simulations. 0 Answers

Overload Operator in UnityScript? 0 Answers

Touch.deltaTime confusion 0 Answers

Time.deltaTime help 1 Answer

Delta time on older computers messed up 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