- Home /
Is there any way to apply drag to a 3d rigidbody in only 1 direction?
I am making a driving game without wheel colliders and I want the car to have more drag when one is driving but than when the car is in mid air it falls more slowly. I want to make the car have more drag horizontally than forward and backward because wheels only spin in one direction but I cant figure out any way to adjust RigidBody drag on only one axis. i have tried adjusting the cars physics material but that did not work. does anyone have any suggestions? Thanks!
Answer by Eno-Khaon · Jan 01 at 11:25 AM
This doesn't seem like it should be too hard to do overall; you just have to do it yourself.
For reference, the physics/drag implementation reference is based around a post I made on the subject back in 2016. Nothing has changed since then, to my knowledge, but I'm offering this disclaimer just in case.
Ensure that your vehicle has *NO* physics drag. Then, in your vehicle script, just include the way you want drag to be applied instead:
// Example of transferring Rigidbody.drag to a custom variation
Rigidbody rb;
float vehicleDrag;
void Start()
{
rb = GetComponent<Rigidbody>();
vehicleDrag = rb.drag;
rb.drag = 0f;
}
void FixedUpdate()
{
// Example based on PhysX's kinda-lousy implementation
// for consistent behavior
rigidbodyDrag = Mathf.Clamp01(1.0f - (vehicleDrag * Time.fixedDeltaTime));
rb.velocity -= rb.velocity * vehicleDrag * (transform.up + transform.right) * 0.5f;
}
In this example, the drag is scaled by the tangential (up/right) vectors of the vehicle to avoid dampening velocity along the remaining forward vector.
Answer by code_laser · Jan 16 at 02:39 PM
Thanks! implementing my own drag system is a good idea. while i do not think i will use your exact code it is a good start. sorry for the late response. have a good day!
Your answer
Follow this Question
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
F-Zero Game - Collisions/Physics. 1 Answer
Changing rigidbody drag based on deviation of rigidbody heading to its direction 2 Answers