• 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
0
Question by falconstrike209 · Jan 11 at 10:30 PM · unity 2ddragplayer movementx axis

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!

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

2 Replies

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

Answer by jackmw94 · Jan 11 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;
Comment
Add comment · Show 6 · 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 falconstrike209 · Jan 12 at 09:54 PM 0
Share

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.

avatar image falconstrike209 · Jan 12 at 10:19 PM 0
Share

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...

avatar image jackmw94 falconstrike209 · Jan 14 at 10:15 AM 0
Share

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.

avatar image falconstrike209 jackmw94 · Jan 15 at 10:27 PM 0
Share

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?

Show more comments
avatar image falconstrike209 · Jan 17 at 01:33 AM 1
Share

This works like a charm! thanks so much dude!

avatar image
1

Answer by MSavioti · Jan 13 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.

Comment
Add comment · Show 2 · 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 falconstrike209 · Jan 13 at 09:03 PM 0
Share

I was referring to moving the character ;P

avatar image falconstrike209 · Jan 13 at 09:04 PM 0
Share

I'll try to do something like this, thanks

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

119 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 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

Use Rigidbody2D's Drag to slow down flying character 0 Answers

Change the position of the Joystick by tap 2 Answers

Platformer problems 1 Answer

Touch movement 0 Answers

Using rays to spider climb fluidly in 2D 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges