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 PanzerPotato · Jun 16, 2018 at 03:48 PM · physicsvelocityaddforceaccelerationship

Accelerate a rigidbody towards max speed

Hey guys,

I'm currently in the process of creating a naval game, and I have been trying to find a way for the ships (which have rigidbodies) to accelerate or decelerate to a certain speed. Drag will affect the ships. Any ideas on how I would approach this problem?

Thanks!

(By the way, I am aiming for movement similar to that of World of Warships.)

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

Answer by Bunny83 · Jun 17, 2018 at 02:21 PM

Since it seems you use drag, have a look at my answer over here


You may want to use the "GetRequiredAcceleraton" method. Keep in mind that the required force is just the required acceleration multiplied by the object's mass. Though you can simply use ForceMode.Acceleration. When using ForceMode.Force Unity simply divides the incoming force by the mass to determine the acceleration internally.

Comment
Add comment · Show 3 · 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 PanzerPotato · Jun 17, 2018 at 05:59 PM 0
Share

Thanks for your reply, it works beautifully! One thing though, is there any way I can slow the acceleration (for a more realistic effect, since my game involves ships) but still reach the target velocity? Edit: Could this be applied to turning?

avatar image Bunny83 PanzerPotato · Jun 18, 2018 at 01:53 AM 0
Share

$$anonymous$$y method just calculates the $$anonymous$$imum required acceleration to sustain the targetspeed. If you want a more realistical behaviour you may want to lower your drag which will automatically lower the required acceleration. On top of that you can always ramp up your actually used acceleration over time until you hit the required acceleration.


Note that Unity's way to calculate drag is not correct. Drag in the real world usually increases quadratically in relation to the velocity. Unity just lowers the velocity by a certain percentage. Ins$$anonymous$$d of using the built-in drag you can set it to 0 and apply your own drag manually.


I'm not sure what you mean by "turning" and what you mean by "applying" in this context.

avatar image PanzerPotato Bunny83 · Jun 18, 2018 at 03:23 AM 0
Share

Well never$$anonymous$$d turning (and slowing the acceleration I just figured out). But thanks for your great response!

EDIT: By turning, I meant to ask if I could use your formulas to accelerate a ship until it turns a specific number of degrees per second.

avatar image
1

Answer by ImpOfThePerverse · Jun 16, 2018 at 05:28 PM

It's called a PID loop, though you'll only be implementing the "D" (derivative) part of it. You compute a Vector3 that is the target velocity (how fast you want the ship to go). Subtract the ship's actual velocity from it, and multiply by a constant to get a force. The force will be high when the ship is far from the target velocity, and zero when the two match.

 Vector3 targetVelocity = transform.rotation * Vector3.forward * targetSpeed;
 Vector3 force = (targetVelocity - rb.velocity) * forceMult;
 rb.AddForce(force);

Edit - Since you're modeling ships, you might want to clamp the force so that it doesn't exceed the maximum thrust of the ship.

 if (force.magnitude > forceMax)
 {
     force = force.normalized * forceMax;
 }

Comment
Add comment · Show 8 · 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 PanzerPotato · Jun 16, 2018 at 05:43 PM 0
Share

Thanks for the reply! So just to make sure,

  1. targetVelocity can be a constant,

  2. force$$anonymous$$ult controls how quickly my ship speed up,

  3. And I can clamp the maximum amount of force used?

avatar image ImpOfThePerverse PanzerPotato · Jun 16, 2018 at 06:20 PM 0
Share

targetSpeed is a constant, a public variable set in the inspector or calculated from stats and user input (like all ahead full, impulse, etc.). targetVelocity gets computed each frame, since even if the targetSpeed hasn't changed, the ship's rotation might have. To clamp the acceleration to the ship's maximum thrust, insert the second bit of code I edited in there in between lines 2 and 3. So you calculate target velocity, then calculate the force, then clamp the force with the inserted code, then apply the force. force$$anonymous$$ult deter$$anonymous$$es how quickly ships speed up, with force$$anonymous$$ax limiting it based on the ship's maximum thrust.

avatar image PanzerPotato ImpOfThePerverse · Jun 16, 2018 at 09:13 PM 0
Share

Oh I see. Thanks mate!

Show more comments
avatar image Bunny83 · Jun 17, 2018 at 02:46 PM 0
Share

I just want to add that this is not the "D" part of a PID controller but just the "P" part. However if you want to use a PID controller to cope with drag you need at least a PI controller. The D part is not really required as it mainly handles sudden changes in the error. So the faster the error goes up / down the more correction is applied in order to damp the error increase. The P (propotional) part is the usual direct correction that you actually do in your code. Wikipedia actually has a decent description of the 3 parts.


The tricky part when using a PID controller is to fine tune the 3 parts to represent your desired behaviour.

avatar image PanzerPotato Bunny83 · Jun 17, 2018 at 04:55 PM 0
Share

Thanks for your reply, but I can't understand it with -100 IQ! :D

avatar image Arman-Mokammel PanzerPotato · Jun 23, 2018 at 04:09 PM 0
Share

hello can you help with my issue please (Link to my issue)

Show more comments

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

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

Related Questions

Obtaining Constant Speed with AddForce 1 Answer

Why use AddForce rather than modify velocity? 4 Answers

Character accelerates while running towards walls. "Sonic effect" 1 Answer

arrows in unity 3d? 0 Answers

How can I convert velocity/direction to Force? 3 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