Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 _watcher_ · Apr 22, 2016 at 07:36 PM · jumpingtime.deltatimeindependent

Yet another Time.deltaTime question

Jumping and framerate independence. Tried solutions, didn't like.

Here is simple example that demonstrates the point - empty scene, attached this to gameobject:

 using UnityEngine;
 using System.Collections;
 
 public class jumpTest : MonoBehaviour
 {
     private float velocityY = 0;
     private int counter = 0;
     private float jumpHeight = 0.1f;
     private Vector3 gravity = new Vector3(0,-.1f,0);
 
     // Update is called once per frame
     void Update ()
     {
        // gravity framerate independent
        float gravityY = gravity.y * Time.deltaTime;
         
        if (counter == 0)
         {
             // Jump is impulse, wont mult jumpHeight
             // @see http://answers.unity3d.com/questions/1156824/forcemode2dimpulse-do-we-need-timedeltatime.html
             // velocity framerate independent
             velocityY += (jumpHeight + gravityY);
         }
         else
         {
             velocityY += gravityY;
         }
 
         if (velocityY < 0)
         {
             velocityY = 0;
         }
 
         transform.position = new Vector3 (
              transform.position.x,
              transform.position.y + velocityY,
              transform.position.z
          );
 
         counter++;
     }
 }
 

So what do we have here - first frame execution - add "upward impulse" to gameObject. future frames: applies gravity to the gameobject, so that at some point going up, the gravity takes over and gameObject starts falling down. As soon as we register falling down, we STOP! the simulation and check the Y position.

I expect if this was framerate-independent we would get approximately the same Y position from multiple runs (difference in +/- size of last deltaTime as the update ticks differ from run to run), However, this aint the case. The y-position differs wildly from run to run. Also on mobiles where framerate dips lower, Y-position is higher. Framerate-dependent. How do we make this piece of code FPS independent?

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
Best Answer

Answer by Bunny83 · Apr 23, 2016 at 02:49 PM

Actually doing what you did in your answer is what most people do. However it's still not frame independent. The error is quite small but still there. For most singleplayer applications the error can be ignored. However if we talk about multiplayer you want to ensure that all players get the same values.

Here's an example table of how time, acceleration, speed and position of an object advance depending on the way they are calculated. The assumed framerate is 10 frames per second for this example.

 deltaTime(dt) = 0.1 (== 10fps)
 time    acc     speed    pos1   pos2   pos3(old  + last + cur  == new)
 0.0     2       0.0      0.0    0.0         0.0
 0.1     2       0.2      0.02   0.02        0.0  + 0.0  + 0.01 == 0.01
 0.2     2       0.4      0.06   0.04        0.01 + 0.01 + 0.02 == 0.04
 0.3     2       0.6      0.12   0.09        0.04 + 0.02 + 0.03 == 0.09
 0.4     2       0.8      0.2    0.16        0.09 + 0.03 + 0.04 == 0.16
 0.5     2       1.0      0.3    0.25        0.16 + 0.04 + 0.05 == 0.25
 0.6     2       1.2      0.42   0.36        0.25 + 0.05 + 0.06 == 0.36
 0.7     2       1.4      0.56   0.49        0.36 + 0.06 + 0.07 == 0.49
 0.8     2       1.6      0.72   0.64        0.49 + 0.07 + 0.08 == 0.64
 0.9     2       1.8      0.9    0.81        0.64 + 0.08 + 0.09 == 0.81
 1.0     2       2.0      1.1    1.0         0.81 + 0.09 + 0.1  == 1.0
 1.1     2       2.2      1.32   1.21        1.0  + 0.1  + 0.11 == 1.21
 1.2     2       2.4      1.56   1.44        1.21 + 0.11 + 0.12 == 1.44
 1.3     2       2.6      1.82   1.69        1.44 + 0.12 + 0.13 == 1.69
 1.4     2       2.8      2.1    1.96        1.69 + 0.13 + 0.14 == 1.96
 1.5     2       3.0      2.4    2.25        1.96 + 0.14 + 0.15 == 2.25
 1.6     2       3.2      2.72   2.56        2.25 + 0.15 + 0.16 == 2.56
 1.7     2       3.4      3.06   2.89        2.56 + 0.16 + 0.17 == 2.89
 1.8     2       3.6      3.42   3.24        2.89 + 0.17 + 0.18 == 3.24
 1.9     2       3.8      3.8    3.61        3.24 + 0.18 + 0.19 == 3.61
 2.0     2       4.0      4.18   4.0         3.61 + 0.19 + 0.2  == 4.0
 
 
 [pos1] -- the usual way to calculate movement
 speed = speed + acc * dt
 pos = pos + speed * dt
 
 [pos2] -- the correct values determined by absolute time passed (t)
 speed = acc * t
 pos = (acc/2) * t²
 
 [pos3] -- the tricky way how to get correct values when calculating accumulative each frame
 pos = pos + (speed/2)*dt
 speed = speed + acc*dt
 pos = pos + (speed/2)*dt

As you might remember from physics class this is how you calculate the position if you have a uniform acceleration. This value is shown in the table as [pos2]. This is not caculated accumulative as we have to do it. Since the real world doesn't work in "frames" our splitting of the time into frames cause a lot of trouble. A linear change can be scaled by deltaTime without any problems since scaling by dt is a linear operation. However it doesn't work for a quadratic progression since we can't assume the values in between two points are linearly distributed which we do when we calculate the position as shown in [pos1].

The actual trick to get the correct answer independent from the framerate is to do this each frame:

  • add half of the last speed multiplied by "deltaTime" to pos.

  • update speed as usual by adding the acceleration multiplied by "deltaTime".

  • once more add half of the current speed multiplied by "deltaTime" to pos.

So it would look like this:

 Vector3 velocity = Vector3.zero;
 
 void Update ()
 {
     transform.position += velocity*Time.deltaTime / 2;
     velocity += gravity * Time.deltaTime;
     if (shouldJump)
         velocity += Vector3.up * jumpHeight;
     transform.position += velocity*Time.deltaTime / 2;
 }

Note: adding a jump impule should be done only in one frame. This force should not be multiplied by Time.deltaTime. "shouldJump" is simply your condition when you want to perform a jump. This should be true for one frame.

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 _watcher_ · May 01, 2016 at 08:11 AM 0
Share

Very throughout, appreciate it. Here, have a cookie <3.

avatar image
0

Answer by _watcher_ · Apr 23, 2016 at 10:49 AM

Here is the solution:

Change:

      transform.position = new Vector3 (
           transform.position.x,
           transform.position.y + velocityY,
           transform.position.z
       );

To:

          transform.position = new Vector3 (
               transform.position.x,
               transform.position.y + velocityY * Time.deltaTime,
               transform.position.z
           );





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

how to make player run-speed independent of computer? 0 Answers

Jumping Issue on Different Computers 0 Answers

FixedUpdate and Time.deltaTime slowe 2 Answers

Character floats upward when looking up and down 0 Answers

How can I stop the fps controller jumping up slopes? 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