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 haskell33 · Jul 17, 2014 at 06:17 AM · rigidbody2dtimephysics2dfixedupdatedeltatime

Odd character dash discrepancies. Time.deltaTime?

Hey guys, so here's the deal. I have a dash counter, dashTimeMax (how long a dash should take. set to 0.5 seconds) and a dash speed, dashSpeed, of 4 mps. So, in theory, I'm expecting my character to move 2 meters after 1 dash execution. Thing is, I'm getting results that go from 1.92 (maybe less) meters to 2.08 meters, and I don't know why?

Here's my code:

On Update:

 void UpdateDashStatus()
     {
         if(grounded && downButtonPressed && jumpButtonPressed && !isDashing)
         {
             isDashing = true;
 
             jumpButtonPressed = false;
             
             dashTimeCounter = dashTimeMax;
         }
         
         if(isDashing)
         {
             dashTimeCounter = dashTimeCounter - Time.deltaTime;
 
             if(dashTimeCounter <= 0f)
             {
                 //print (Time.deltaTime);
                 //print (transform.position.x);
                 isDashing = false;
             }
         }

     }

On FixedUpdate:

 void PerformDash()
     {
         if(isDashing)
         {
             if(facingRight)
                 rigidbody2D.MovePosition(rigidbody2D.position + new Vector2(dashSpeed, 0) * Time.deltaTime);
             else
                 rigidbody2D.MovePosition(rigidbody2D.position + new Vector2(-dashSpeed, 0) * Time.deltaTime);
         }
     }

Shouldn't the uses of deltaTime, and of PerformDash() inside of FixedUpdate be giving me sharp results?

If needed, I'll try to provide further details. Thanks.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Tehnique · Jul 17, 2014 at 06:44 AM

Well, you do execute your dash OnFixedUpdate, and you update your condition on Update. This can cause certain discrepancies, especially for small values of dashTimeMax, like your case.

For example at Update, you check dashTimeCounter and it is >0, so you keep isDashing as true. Later, on FixedUpdate, you execute your dash, but more time has passed since Update, so maybe your dash should have ended, but your condition was evaluated earlier.

You could try to move your Update code to FixedUpdate to decrease your error.

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 haskell33 · Jul 17, 2014 at 07:12 AM 0
Share

I've done as you've said, Tehnique, passing just the lines:

 dashTimeCounter = dashTimeCounter - Time.deltaTime;
  
 if(dashTimeCounter <= 0f)
 {
 isDashing = false;
 }

to FixedUpdate, since the other part was Input handling, and it worked like a charm. Thank you very much!

On a sidenote: The example presented here, using deltaTime without physics manipulation and inside Update:

http://docs.unity3d.com/ScriptReference/Time-deltaTime.html

It isn't 100% accurate (small differences, though). Is that normal behaviour?

avatar image Tehnique · Jul 17, 2014 at 07:45 AM 0
Share

I'm glad it worked. As for your last question, do you mean that in the example, the object does not move 10m/s?

I'm guessing it is normal to have small differences, depending on when you measure the movement (if it is between updates, it's normal).

To clarify a few things: FixedUpdate should run every 0.02 seconds, at least that's how Unity tries to run it. Update, on the other hand runs every frame. So, if you are above 50 FPS, Update runs more often, if you are below, FixedUpdate runs more often. That's jsut a comparison, the 2 functions are not interchangeable, especially because FixedUpdate should be kept very lightweight, and only for Physics stuff.

avatar image haskell33 · Jul 17, 2014 at 08:13 AM 0
Share

Yes, I was saying that the example, as presented on that link, always demonstrates a little discrepancy. If I print time.Time and transform.position.x both inside Update, it reaches 10+ (let's say 10.1) at 0.9. I don't see how this is possible, since I'm using deltaTime? Where does the remainder came from? Also, thanks for the information about FixedUpdate and Update.

avatar image Tehnique · Jul 17, 2014 at 08:44 AM 0
Share

Time.time is only calculated once/frame and it returns the same value throught the frame. So your update can come later in the frame, but Time.time will return the same value that was calculated before, hence Update moves the transform according to the real time since last frame, but Time.time returns a smallver value of time.

avatar image
0

Answer by Guambo1 · Mar 09, 2020 at 09:21 PM

I had a similar issue. I moved my dash related code to fixedupdate and now I get a consistent dash distance. However, now the player stutters when they dash. Have you had a similar issue? @haskell33

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

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

Related Questions

How to add Hit Stop Frames? 0 Answers

Problem with Fixed Timestep 0 Answers

How often does the internal physics calculation get called? 1 Answer

How to compensate Fixed TimeStep - speed in game 0 Answers

Rigidbody2d.MovePosition on Update vs FixedUpdate 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