• 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 rawalpundi · Aug 16, 2014 at 01:59 PM · force

why using DeltaTime with AddForce ?

Hi all, I'm a beginner and I am following the Roll-a-Ball project tutorial. In the "Moving the player" step, he asks to use AddForce with Time.deltaTime to be frame independent. I don't understand why to use deltaTime. The manual says the AddForce parameter is either a force (mass.meter/second2) or an acceleration (meter/second2). So the parameter should not be a constant ? Why making it dependent of the frame rate ? Thanks

Comment
Add comment · Show 3
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 robertbu · Aug 16, 2014 at 06:02 PM 0
Share

This does seem to be strange to me also. Can you post the relevant code or maybe the whole script. I'd like to see some context.

avatar image rawalpundi · Aug 17, 2014 at 08:58 AM 0
Share

ok, here is the script of the tutorial :

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : $$anonymous$$onoBehaviour
 {
     public float speed;
 
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
         rigidbody.AddForce (movement * speed * Time.deltaTime);
     }
 }
avatar image Huacanacha · Aug 17, 2014 at 09:40 AM 1
Share

Force causes acceleration which only has an effect over time. I suspect the AddForce method doesn't make assumptions about where it is called from (FixedUpdate, Update, or outside of an update) and therefore uses a default value of 1 second when applying the acceleration changes due to the force. This therefore requires scaling by deltaTime if you intend to apply a force for the duration of an Update or FixedUpdate.

This applies to the standard case with Force$$anonymous$$ode.Force. I admit I'm a little confused myself about Force$$anonymous$$ode.Impulse

4 Replies

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

Answer by robertbu · Aug 18, 2014 at 05:46 AM

First, deltaTime is reset to fixedDeltaTime when used inside Update. From the manual:

When called from inside MonoBehaviour's FixedUpdate, returns the fixed framerate delta time.

So the tutorial is wrong. The only reason I could see for using Time.deltaTime in FixedUpdate() would be to match real-world physics. The use of 'speed' here is also wrong. You are applying force. So if you wanted to apply a measured force in Newtons to a known mass of the object, scaling by deltaTime would make the resulting acceleration be a much closer match to what you would get if you used a formula to calculate the acceleration.

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 rawalpundi · Aug 20, 2014 at 06:13 PM 0
Share

Ok robertbu, I agree with you. There is no need to use deltaTime with AddForce. I have made a little experiment with only gravity on a sphere, and check which value must be applied in AddForce to get the sphere motionless. The result is that we only need to input a force in newtons, no need to scale with deltaTime.

avatar image
1

Answer by angularsen · Apr 02, 2018 at 04:36 PM

TL;DR You do NOT need to multiply by deltaTime.


I just looked into this by creating a simple script, and found that FixedUpdate() indeed does add forces as if the delta time between each call to FixedUpdate()was multiplied into it.


Game object:

  • Mass m: 1

  • Gravity: 0

  • Linear/angular drag: 0

  • Position: (0, 0)

  • Force F: (0, 1)

  • Vertical acceleration: a = F/m = 1 / 1 = 1


By linear motion equations, t=5 seconds of vertical thrust should give velocity Vy = a*t = 1*5 = 5 and position Py = 0.5*a*t* = 0.5*1*5*5 = 12.5.


I tried running this with target framerate 5 FPS and 60 FPS, as well as fixed timestep 0.005 and 0.05s. The logs of the script matched the above calculations every time, meaning we DON'T need to multiply any time delta inside FixedUpdate().


 using System;
 using UnityEngine;
 
 public class AddForce : MonoBehaviour
 {
     private Rigidbody2D _body;
     public Vector2 Force;
     private float _lastTime;
 
     public void Start()
     {
         _body = GetComponent<Rigidbody2D>();
         if (_body == null) throw new Exception("No RigidBody2D attached.");
         _lastTime = Time.time;
     }
 
     public void FixedUpdate()
     {
         _body.AddForce(Force);
         Debug.LogFormat("Time: {5}, Add force {0}, velocity {1}, pos {2}, fixed time delta {3}, time since last {4}", Force, _body.velocity, _body.position, Time.fixedDeltaTime, (Time.time - _lastTime), Time.time);
         _lastTime = Time.time;
     }
 }


Example output with timestep 0.05:


 Time: 4.95, Add force (0.0, 1.0), velocity (0.0, 5.0), pos (-15.0, 12.4), fixed time delta 0.05, time since last 0.05000019
 UnityEngine.Debug:LogFormat(String, Object[])
 AddForce:FixedUpdate() (at Assets/Scripts/AddForce.cs:20)
 
 Time: 5, Add force (0.0, 1.0), velocity (0.0, 5.0), pos (-15.0, 12.6), fixed time delta 0.05, time since last 0.04999971
 UnityEngine.Debug:LogFormat(String, Object[])
 AddForce:FixedUpdate() (at Assets/Scripts/AddForce.cs:20)
 
 Time: 5.05, Add force (0.0, 1.0), velocity (0.0, 5.1), pos (-15.0, 12.9), fixed time delta 0.05, time since last 0.05000019
 UnityEngine.Debug:LogFormat(String, Object[])
 AddForce:FixedUpdate() (at Assets/Scripts/AddForce.cs:20)

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 DAVco · Sep 10, 2020 at 06:33 PM 0
Share

This always catches me out. To add to @angularsen 's explanation:

All forces applied to a rigidbody are cleared at the start of a FixedUpdate, therefore any force you apply is implicitly applied for the duration of the FixedUpdate (i.e. Time.deltaTime) by the rigidbody.

So again; no need to manually multiply in Time.DeltaTime when applying forces in FixedUpdate

avatar image Bunny83 DAVco · Sep 10, 2020 at 11:38 PM 0
Share

Right, I explained the relationship of the different forcemodes over here.

avatar image
1

Answer by Kiwasi · Aug 20, 2014 at 07:52 PM

The plot has been missed entirely on this one.

The reason to use Time.deltaTime in any function is to make it frame rate independent.

As FixedUpdate() runs at a fixed frame rate there are many people who will advise you not to bother with Time.deltaTime.

However changing the fixed frame rate is common when adjusting game speed, or for late stage optimisation. You really don't want to have to adjust every line involving AddForce if you decide just before launch that you want a time step of 0.05 instead of 0.02.

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

Answer by dwengs · Aug 16, 2014 at 06:17 PM

Maybe I'm wrong but you should check this: http://unity3d.com/learn/tutorials/modules/beginner/scripting/delta-time

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 robertbu · Aug 16, 2014 at 06:20 PM 0
Share

The code in the video is a good use of Time.deltaTime, but the specific question above i about the use of AddForce() and deltaTime. Typically AddForce() when repeated over a number of frames is done in FixedUpdate() where 'Time.deltaTime' is redefined/reassigned to Time.fixedDeltaTime.

avatar image rawalpundi · Aug 17, 2014 at 12:40 AM 0
Share

yes I understand the use of deltaTime when the script controls the "position" of the object because the position depends on the time. But for a force ... if we would like to apply a constant force to an object, for instance 10 newtons, the force will not change depending on the frame rate, it is still 10 newtons

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems making a glider 3 Answers

rigidbody problem 1 Answer

Unity 2D Apply force in Z Direction 0 Answers

There is no 'Rigidbody', but there is. 1 Answer

how to restrict velocity to specific magnitude when force applied equals rigid.mass * 9.8f to fly an object affected by gravity. 0 Answers


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