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 Nashtanir · Feb 21, 2014 at 09:53 PM · frameratetime.deltatimeinteger

Using Time.deltaTime when incrementing a value

I'm incrementing an int variable (kind of a heat system) each time player jumps, keeps holding the jump key and when he moves around. I use addForce method to move the character. All this is happening inside a FixedUpdate function. Do I need to use Time.deltaTime when I'm incrementing or reducing the int value? How do I generally know when to use time.deltaTime?

Below there is a code example of the jump block I have in my character controller script.

 if(Input.GetButton("Jump")) {
   GetComponent<HeatController>().GainHeat(50);    // Is time.deltaTime needed here?
   rigidbody2D.AddForce(new Vector2(0f, jumpForce));
 }
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
0
Best Answer

Answer by rutter · Feb 21, 2014 at 10:05 PM

Values that change over time should generally be expressed in units-per-second. If they aren't, they will implicitly depend on your frame rate, which can vary wildly.

Time.deltaTime tends to be very small. For example, let's pretend that your game runs at exactly 50 FPS (it won't, but it makes the math easy). That gives you about 0.02 seconds per frame. Let's say you want to gain 100 heat per second, while whatever thing is running. Multiplying that 100 by 0.02 gives 2 heat per frame, running that for 50 frames gives 100 heat total.

This stuff gets more intuitive if you learn calculus. :)

This does bring us to one other problem, ints versus floats:

 int x = 1f * 0.5f; //result is "truncated", x is now zero
 float y = 1f * 0.5f; //result is correctly 0.5f

Integers don't ever store a fractional part of your numbers; they "truncate" or discard it completely. Values which need to store fractional parts should usually be floats. If you need to know more about this, most people first run into it with a concept called "integer division", which you can read up on.

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 Nashtanir · Feb 22, 2014 at 09:29 AM 0
Share

How does Unity's FixedUpdate affect to this? I've heard it has something to do with framerate.

avatar image Bunnybomb7670 · Feb 22, 2014 at 10:00 AM 0
Share

FixedUpdate is called every physics step, which is less frequent than every frame, this is useful for intergrating math with physics because modifying physics related stuff inside Update will cause weird things to happen.

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

20 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

Related Questions

Framerate, Time.deltaTime and moving the character. 1 Answer

Problem with jumping and frames per second 4 Answers

Problems with player movement at higher frame rates (Time.deltaTime not producing consistent results) 1 Answer

Using Time efficiently regardless of current frame rate. 0 Answers

I want to make my 2D rigidbodies move smoother! 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