• 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
Question by Kennyist · Oct 12, 2013 at 04:43 PM · c#transformmathvectorsother

How would you fake air resistance? (c#)

Ive got a helicopter using two speeds, Helispeed (pitch foward/back speed) and heliRollSpeed (roll left/right speed), But im trying to add a air resistance element to them so you start to slow down if you are leveling out the heli, But cant work out how to add this in well.

This is how i get the speeds (almost identical for ptich/roll):

     pitch = heli.transform.eulerAngles.x;
         
         if((pitch > 0) && (pitch < 90)){
             
             float temp = (60 - Mathf.Abs((pitch - 45) / 0.75f)) / 100;
             
             heliSpeed = heliSpeed + temp;
             
             if(heliSpeed > 40.0f) { heliSpeed = 40.0f; }

             
             
         } else if((pitch > 180) && (pitch < 360)) {
             
             float temp = (60 - Mathf.Abs((pitch - 405.0f) / 0.75f)) / 100;
             
             heliSpeed = heliSpeed + temp;
             
             if(heliSpeed < -40.0f) { heliSpeed = -40.0f; }
             
         }
         
         if(heliSpeed < 0){
             heli.transform.Translate(-Vector3.forward * -heliSpeed * Time.deltaTime);
         } else {
             heli.transform.Translate(Vector3.forward * heliSpeed * Time.deltaTime);
         }

But the speed will always increase if you are not at 0 degrees. So i tried adding this to a lateUpdate

 if(heliSpeed > 0.01){ heliSpeed -= (heli.rigidbody.drag / heliSpeed); } else if (heliSpeed < -0.01) { heliSpeed += (heli.rigidbody.drag / heliSpeed); }
     if(helirollSpeed > 0.01){ helirollSpeed -= (heli.rigidbody.drag / helirollSpeed); } else if (helirollSpeed < -0.01) { helirollSpeed += (heli.rigidbody.drag / helirollSpeed); }

but without thinking, this just means you cant move in any direction, it just shoots you in the opposite direction.

Comment
teh1archon

People who like this

1 Show 0
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
Best Answer

Answer by Tomer-Barkan · Oct 12, 2013 at 05:35 PM

Rigidbodies have a drag value, which is exactly that:

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-drag.html

If you'd want to do this manually for some reason, simply apply force in the direction opposite of the velocity, in proportion to the current speed. If you care about exact formulas, learn about them here:

http://en.wikipedia.org/wiki/Drag_(physics)

Comment
Kennyist
Avocco
teh1archon
weizenhuhn

People who like this

4 Show 4 · 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 Kennyist · Oct 12, 2013 at 07:37 PM 0
Share

So ive got this:

 drag = 0.5f * 1.2041f * (heliSpeed * heliSpeed) * 0.004f * (4.0f * 4.0f);
         
 Debug.Log("Speed = "+heliSpeed+" -- Drag = "+drag);
         
 if(heliSpeed < 0){
     heli.transform.Translate(-Vector3.forward * -heliSpeed * Time.deltaTime);
     heli.transform.Translate(Vector3.forward * -drag * Time.deltaTime);
 } else {
     heli.transform.Translate(Vector3.forward * heliSpeed * Time.deltaTime);
     heli.transform.Translate(-Vector3.forward * drag * Time.deltaTime);
 }

But it keeps going intil it stops the heli and starts to reverse, how would i change this so it creates a top speed and stays at that?

avatar image Tomer-Barkan · Oct 12, 2013 at 09:02 PM 0
Share

Again - if you use a Rigidbody, the physics system will do it all for you, that's what its there for and that's what I recommend.

If you still want to do it manually - add to your original code something that slows the speed:

 if (heliSpeed > 0) {
     heliSpeed -= drag;
 } else if (heliSpeed < 0)
     heliSpeed += drag;
 }
avatar image Kennyist · Oct 12, 2013 at 09:19 PM 0
Share

Rigid body drag does nothing, thats why im trying to do a manual version. But that seems to be wrong as-well as, as I lift the pitch you slow down instantly, not slowly like you really would.

avatar image Tomer-Barkan · Oct 12, 2013 at 09:23 PM 0
Share

If you want rigidbody drag to work use the physics to move the object.

Instead of using transform.Translate(), use rigidbody.AddForce(), or even set the velocity manually with rigidbody.velocity = something;. That way the physics will take care of changing the position, so you don't need to calculate the speed yourself, and also it will take care of drag (make sure drag is not set to 0 though).

Also it will be easier, simply apply the force to transform.up, instead of calculating the pitch manually, as a real rotor would:

 rigidbody.AddForce(transform.up * throttle);
avatar image

Answer by GibTreaty · Oct 12, 2013 at 05:37 PM

I made a post on the forums about applying drag manually with an equation that works exactly like Unity's.

http://forum.unity3d.com/threads/198252-Applying-Drag-Unity-Style?p=1344567#post1344567

Basically this...

 velocity -= Vector3.ClampMagnitude(velocity, 1) * drag * Time.deltaTime;
Comment
teh1archon

People who like this

1 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 Tomer-Barkan · Oct 12, 2013 at 05:40 PM 0
Share

Does unity not take into account the surface area, like in real physics? IE bullet has less drag than a large box...

avatar image GibTreaty · Oct 12, 2013 at 05:44 PM 0
Share

True but you can still fake it by changing the amount of drag based on your angle (pitch, for example) and velocity. Vehicle drag is done in a similar way in Edy's Vehicle Physics by increasing the amount of drag as your velocity increases.

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

17 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

Related Questions

How to find an intercept on a moving target 2 Answers

How to Reverse an equation.. (c#) 2 Answers

Distribute terrain in zones 3 Answers

Snapping Connection Points Together Rotation 0 Answers

Find Vector3 perpendicular to Vector3 A in direction of Vector3 B 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