Search Unity

Question Is it possible to have a different rigidbody drag value for each axis?

Discussion in 'Scripting' started by imaginereadingthis, Mar 13, 2023.

  1. imaginereadingthis

    imaginereadingthis

    Joined:
    Jul 25, 2020
    Posts:
    97
    Hello everyone,

    I am currently making a first-person dynamic rigidbody-based movement system for my 3D game. So far, I have been using the default Rigidbody.drag property to handle drag. Cranking it up is a quick way to fix that slippery movement we all dread, but it simultaneously causes a lot of other issues.

    For example, if you try to jump up with a very high drag value, the player will basically teleport into the air (assuming you also crank up the jump force) before slowly falling back down like a feather. At that point, even gravity starts to feel like it's constant instead of accelerating. I guess a solution would be to only apply drag to the x and z axis, and set the drag on the y axis to zero or a very low value.

    I know it's not possible to directly change a rigidbody's drag for each axis because it's a global value that affects every axis equally, but is there a way around this? I love the simplicity of rigidbody drag, but I want to disable it just for the y-axis so that the player can jump and fall normally. If this is straight up impossible or just a very bad thing to do, I guess I could make my own custom drag system, but I still want to find a way to mess with rigidbody drag.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    To have different drag values in each axis would just involve handling the movement dynamics yourself, rather than relying on rigidbody physics entirely.

    You can still use a rigidbody, but you would be setting the
    .velocity
    property, or using
    Rigidbody.MovePosition()
    after calculating your own movement values.
     
    imaginereadingthis likes this.
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    imaginereadingthis likes this.
  4. imaginereadingthis

    imaginereadingthis

    Joined:
    Jul 25, 2020
    Posts:
    97
    Now that I think about it, I can just lower the speed with which the player travels in the air to compensate for the low drag value since the issue with just setting the drag to a low value in the air was that the player would glide around really fast. How did I not think of this in the first place? Sometimes I'm just -3 IQ lmao
     
  5. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    989
    As someone who has done this recently I would suggest that you ignore both the built-in gravity and the built-in drag. They will cause more issues than they are worth and writing your logic for both will not only give you more control but also likely take less code than if you tried to force workarounds in. Your best bets are what spiney199 suggests about writing your own custom drag. You can also use tricks like changing the physics materials used by your character's collider(s) based on states like input and whatnot.
     
    imaginereadingthis likes this.