- Home /
Can I Prevent Rigidbody Drag from Affecting the Z-Axis?
I'm currently using Drag in my player gameobject's Rigidbody to limit how far the player is pushed after being struck with a moving projectile. However, this is also affecting the player's jump function, causing it to sort of glide in the air as the drag makes it descend slowly. Is there a way I can prevent the drag from affecting the z-axis so this doesn't happen? Or perhaps another way to limit the push from projectile collisions so I don't need to use drag at all?
Try playing with the Physic$$anonymous$$aterial of both your player, and the objects hitting the player
Answer by Owen-Reynolds · Jan 21, 2016 at 03:33 AM
Drag is just multiplying velocity by almost 1. You can set Physics drag to 0 and apply drag yourself in FixedUpdate. I do things like this when I need a particular "feel":
Vector3 vel = GetComponent<RigidBody>().velocity;
vel.x*=0.98f; vel.y*=0.98f;
GetComponent<RigidBody>().velocity = vel;
Then you can do little tricks, like increasing it after you've flown for a second or more:
float drag=0.98f; if(Time.time+1.5f>lastBumpTime) drag=0.95f;
// apply drag:
Answer by csgeorge · Jan 21, 2016 at 05:20 PM
Thanks for your suggestions, but I found the most effective solution- for my case at least- is to just use Drag and change the physics.gravity of the desired axis to essentially cancel it out.
If you look at changing gravity threads here, they describe a possible problem with that -- everything else also falls faster. So you're back to using code, but this time to increase your own gravity (I think2D has a personal gravity scale, but not 3D.)