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
1
Question by boddole · Oct 07, 2014 at 07:20 AM · physicsdragphysx

Question on Negating Physics Drag

Hello everyone, if / how I could make a non-kinematic rigid body be able to accelerate at a set rate regardless of drag. Essentially taking something like this:

thisRigidbody.AddForce (acellerationSpeed, ForceMode.Acceleration);

And having it become something like this:

thisRigidbody.AddForce (acellerationSpeed + the current Drag value retarding acceleration, ForceMode.Acceleration);

What I'm trying to achieve: Essentially I have a non-kinematic rigid body boat, and I would like to be able to have it accelerate at a constant rate regardless of Drag up to a maximum speed (or a % of that max speed), but still have drag so that when the acceleration conditions are no longer met, the boat will slow down (and I can change how fast it does that with Drag).

You'll see below that I have done a simple if / else where if the boat is accelerating the Drag is 0, then when the player is trying to slow down, Drag is applied. It works until the boat turns, and of course at that point since Drag is 0 it will drift endlessly...so it has some drawbacks.

     void Update () 
     {
         if(slider.value > 0f && thisRigidbody.velocity.z < (maxSpeed * slider.value) && thisRigidbody.velocity.z < maxSpeed)
         {
             //thisRigidbody.drag = 0f;
             time += Time.fixedDeltaTime;
             //Debug.Log ("maxspeed * slider.value is:" + (maxSpeed * slider.value));
             thisRigidbody.AddForce (acellerationSpeed, ForceMode.Acceleration);
         }
 
         /*
         else if (thisRigidbody.velocity.z > (maxSpeed * slider.value))
         {
             thisRigidbody.drag = 0.2f;
         }
         */
 
         Debug.Log ("Current fixed time is: " + time);
         Debug.Log ("Current Cube Velocity is: " + thisRigidbody.velocity);
     }

Any ideas are appreciated, thank you for reading.

Comment
Add comment · Show 2
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 Imankit · Oct 07, 2014 at 09:44 AM 0
Share

You can directly add velocity to the rigidbody lets assume you want to move your boat in z direction

 rigidBody.velocity = new Vector3(0,0,acellerationSpeed);

This willmake your boat go in a constant speed.

And when you want to slow down your boat just reduce the velocity

avatar image boddole · Oct 07, 2014 at 10:49 AM 0
Share

True enough, I was just hoping to not have to worry about slowing the object down manually...I probably just being lazy.

1 Reply

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

Answer by Bunny83 · Oct 07, 2014 at 12:33 PM

It's way easier to apply a directed drag yourself:

 void FixedUpdate()
 {
     var vel = rigidbody.velocity;
     var localVel = transform.InverseTransformDirection(vel);
     var drift = localVel;
     // remove the forward component when accelerating
     if (accelerating)
         drift.z = 0;
     // transform back to worldspace and invert
     var drag = -transform.TransformDirection(drift);
     drag *= someDragFactor * Time.deltaTime;
     rigidbody.velocity = vel + drag;
 }

This is just some kind of pseudo code. "accelerating" is your condition if you accelerate at the moment and "someDragFactor" is a float you might want to modify to control the amount of drag.

Note: When dealing with continous forces or velocity you should do your calculations in FixedUpdate. FixedUpdate is the physics update loop. It ensures a constant behaviour regardless of the framerate.

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 boddole · Oct 09, 2014 at 04:34 AM 0
Share

Interesting, I'll give it a shot, thanks.

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

30 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

Related Questions

Moving a rigidbody onto exact mouse position using rigidbody.MovePosition? 1 Answer

How does Unity3d uses nvidia 0 Answers

Rope Physics - PhysX - Box2X - Chipmunk 2 Answers

C# Check Physics.Raycast Once 0 Answers

How does Unity3d uses nvidia's PHYSX engine? 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