- Home /
How do I only add drag on the x axis in unity2D?
I have a player. The movement code is using AddForce. (I need to use AddForce because of other physics in the game, please don't just tell me to switch to velocity.) I wanted the player to come to an immediate stop. I looked up how to do this, and found I can simply change the linear drag value on the Rigidbody2D. However, doing this also added drag on the Y axis, effectively ruining the player's jump/gravity. So I was just wondering if anyone knows of a way to only have drag on the X axis. thanks!
Answer by jackmw94 · Jan 11, 2021 at 10:55 PM
I think I'm missing something about not setting velocity, why won't this do the trick: ?
Vector3 velocity = rigidBody.velocity;
velocity.x = velocity.x / (_xAxisDrag + 1f); // zero = no drag. Start with a small value like 0.05
rigidBody.velocity = velocity;
what I mean by "velocity" is the type of movement commonly used in tutorials, that is usually considered superior to AddForce, however because of the physics in my game, I need to use force instead of velocity. with velocity movement, the when you stop pressing the key the velocity instantly goes back to zero, however, when you are adding force the player accelerates and decelerates. I want the player to instantly stop, and supposedly adding drag is the best way to do it. In the unity editor there is the option for "linear drag" which affects the x axis and the y axis. I need to only have drag on the x axis so jumping and gravity work. I'll try the code you sent now.
Wait, how exactly do I implement this? I was trying to use the inspector to change the properties of drag, I'm unsure of how to use code properly...
In your player movement script add these 3 lines after all the current movement code. "_xAxisDrag" is a serialized field float that you can change in the inspector. Since this is your own implementation of drag, you don't need to mess with unity's drag values so this can be zero so as not to affect this new drag function. "rigidBody" should be the player's rigid body you're adding forces to.
i'm getting an error in unity because "the name _xAxisDrag does not exist in the current context" Is this code meant to work for 2D?
Answer by MSavioti · Jan 13, 2021 at 12:31 AM
I suggest you to do a rb2d.velocity = Vector2.zero
as soon as the player release the key AND is grounded. To prevent zeroing the velocity even when it's idle, you can have a bool _isIdle
that prevents additional and useless attribution to make the player stop moving.
I hope when you said to not suggest using velocity you were referring to moving the character.
Your answer
![](https://koobas.hobune.stream/wayback/20220613023122im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Platformer problems 1 Answer
Change the position of the Joystick by tap 2 Answers
Use Rigidbody2D's Drag to slow down flying character 1 Answer
Touch movement 0 Answers
Using rays to spider climb fluidly in 2D 0 Answers