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 /
This question was closed Sep 01, 2015 at 04:35 PM by aleichert for the following reason:

The problem was found.

avatar image
0
Question by aleichert · Sep 01, 2015 at 04:12 PM · rigidbodyaddforceaccelerationdeltatime

[Solved] Unity rigidbody.addForce ignores time.deltaTime? Possibly just a code bug I cannot find.

EDIT: The issue was resolved quite quickly. The problem was me multiplying the maxSpeedSquared with maxSpeed instead of equaling it to maxSpeed multiplied with maxSpeed. I also learnt something new, so for those that stumbled upon this question in search of an answer for a similar problem should also note that physics should be done in FixedUpdate().

Hello there. I've been trying to do a simple acceleration of a rigidbody and deceleration when I release the acceleration button (in this case the "W" key). Also a top speed and a faster deceleration when I push a button to "brake" (in this case the space bar.)

I got everything working - apart from the acceleration. Somehow, the rigidbody.addForce method I am using combined with time.deltaTIme won't add a force once every second - it adds it constantly, or at least my rigidbody accelerates to maxSpeed instantlyinstantly. Please see the code to see what I did. This might be caused by the way I handle the top speed - however I have been trying to figure this out since over half an hour now and still cannot seem to find the bug in the code. Could you please help me with this? I am sure I'm just overlooking something simple.

 public float speed;
     public float maxSpeed; // The maximal speed
     public float accelerationSpeed; //The acceleration speed (or multiplier)
 
     private Rigidbody rig; // To not always have to do GetComponent()
     private Vector3 speedVec = new Vector3 (0,0,0); // To not always have to make a new vector but rather just keep changing it
     private float maxSpeedSquared; // Since rigidbody.velocity.sqrMagnitude returns a squared number, I need to do the same for my max speed to even it out. (10 turns into 100, while velocity with 10 would be 100 squared, which the sqrMagnitude returns.)
     // Use this for initialization
     void Start () {
         rig = this.GetComponent<Rigidbody> ();
         maxSpeedSquared *= maxSpeed;
     }
     
     // Update is called once per frame
     void Update () {
         speedVec = rig.velocity;
         speed = speedVec.sqrMagnitude;
         if (Input.GetKey (KeyCode.W)) {
             if (rig.velocity.sqrMagnitude > maxSpeedSquared) {
                 rig.velocity = speedVec.normalized * maxSpeed;
             } else {
                 rig.AddForce(transform.forward * accelerationSpeed * Time.deltaTime);
             }
         }
         if (Input.GetKey (KeyCode.Space)) {
             rig.drag = 2; // When no force is added, the rigidbody drag decelerates it, normally it's set to 1 but in order to "brake" e.g. decelerate faster I set it to 2.
         }
     }


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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

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

Related Questions

Rigidbody.AddForce 2d movement without acceleration 2 Answers

Confusion with AddForce with ForceMode.Acceleration 1 Answer

Ball Addforce Acceleration Speed 1 Answer

About Forces and Time.deltaTime 3 Answers

Correct way of calculating forces for rigid bodies 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