• 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
3
Question by Hogge · Apr 02, 2017 at 05:16 PM · physicsaxisdrag

Different drag for each axis

I'm currently working on a flying game. A problem I've been having so far is drag. If I set it too low, the plane "drifts" through the air. If I set it high enough to stop this behavior, the plane stops almost instantly when I turn off the engine. And of course, the plane doesn't gain speed from diving.

So what I figure that my game needs is for the plane to have separate drag values in all axis. Thing is, I'm not really sure how I'm supposed to do that.

So quite simply... how would I do that? Thank you in advance!

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

3 Replies

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

Answer by Bunny83 · Apr 03, 2017 at 04:11 AM

Rigidbodies are always simulated in worldspace. Therefore velocity is also a worldspace direction. You have to convert the velocity into local space, apply your localspace drag and convert it back to world space.

 public class DirectionalDrag : MonoBehaviour
 {
     public float x;
     public float y;
     public float z;
     Rigidbody rb;
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
     }
     
     void Update ()
     {
         Vector3 vel = transform.InverseTransformDirection(rb.velocity);
         vel.x*= x;
         vel.y*= y;
         vel.z*= z;
         rb.velocity = transform.TransformDirection(vel);
     }
 }

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 Hogge · Apr 03, 2017 at 08:57 PM 0
Share

Excellent! Thanks a lot, that did the trick!

avatar image
0

Answer by Hogge · Apr 02, 2017 at 10:15 PM

After doing some research on the forums, I've come across a method and written the following code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DirectionalDrag : MonoBehaviour {
     public float x;
     public float y;
     public float z;
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 vel = GetComponent<Rigidbody> ().velocity;
         vel.x*= x;
         vel.y*= y;
         vel.z*= z;
         GetComponent<Rigidbody> ().velocity = vel;
     }
 }
 

The thing is however, that when I test this, it seems like it works based on global axis, not local. In other words, when I adjust the values to be more than 1, the plane starts to fly in a different direction than it's pointing.

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 LeonmFF · Dec 21, 2020 at 01:40 PM

This is old and already have a solution, but I just can't help myself when I see an avoidable GetComponent in an Update function. Also, as we're are multiplying the values, I added a "1f +", so if you set the drag to 0 at the Inspector, it will still normally moves without drag. Set it to 0.2f and it will have a proper 0.2f drag.

You can also use this one:

 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody))]
 public class DirectionalDrag : MonoBehaviour
 {
     [SerializeField]
     float _xDrag;
     [SerializeField]
     float _yDrag;
     [SerializeField]
     float _zDrag;
 
     Rigidbody _rb;
 
     private void Awake() => _rb = GetComponent<Rigidbody>();
 
     void Update()
     {
         Vector3 t_velocity = _rb.velocity;
 
         t_velocity.x *= 1f + _xDrag;
         t_velocity.y *= 1f + _yDrag;
         t_velocity.z *= 1f + _zDrag;
 
         _rb.velocity = t_velocity;
     }
 }
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 entirelydoomed · Aug 11, 2021 at 12:56 PM 0
Share

Shouldn't we divide by drag instead of multiplying on it? In case when drag is of value 0.2f, the resulting velocity will be 1.2 times bigger while it should be theoretically 1.2 times lower.

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

102 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

Related Questions

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

Making a physics based game run faster 0 Answers

How do drag and angular drag interact? 2 Answers

2D Plane Axis Restriction, Photon Networking & Physics... 0 Answers

Android Moving with finger without breaking physics 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