2
\$\begingroup\$

I'm building a small 2D character controller using Unity physics to simulate an oldschool platformer. I know this is not the best way to do it and I have my own version using just raycast and custom physics.

This morning I was adding the jump and noticed that the rigidbody drag property affects X and Y axis. I mean, if I apply an impulse following up direction and I get a jump of 10 units if I make the drag value higher it will jump lower (less than 10). I don't know why, I was assuming that drag was going to affect just movement in X axis.

So, what I need is a way to keep the jump height independent of the drag value I use to keep things consistent and make tweaking easier.

Is this possible?

\$\endgroup\$
1

2 Answers 2

3
\$\begingroup\$

I can think of two ways to address your problem (while still using Physics2D) :

Increase Gravity

You could try increasing the gravity scale to offset the extra drag.

enter image description here

Implement your Own Drag

You could zero the rigidbody drag factor and instead add a script that adds an force on the x-axis only of the rigidbody that increases with speed.

Here's a (simplistic) script that you could attach to your character with a Rigidbody2D component that will add lateral drag in proportion to lateral velocity:

using UnityEngine;

public class XDrag : MonoBehaviour
{
    Rigidbody2D rb;
    public float dragFactor = 10.0f;


    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }


    void FixedUpdate()
    {
        rb.AddForce(new Vector2(-(rb.velocity.x * dragFactor), 0));
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ I thought about first possility but seems too hacky. The second one could be better but I would like to know how unity applies drag to a rb. It is just a matter of decreasing the velocity magnitude by the dragging? \$\endgroup\$
    – Notbad
    Oct 5, 2021 at 15:40
  • \$\begingroup\$ I agree the first approach feels a little hacky but after doing a few experiments I actually felt it gave better results than the second method. I haven't tried Philipp's suggestion yet to change the drag coefficient during FixedUpdate, but it seems like a promising idea. \$\endgroup\$ Oct 5, 2021 at 16:04
  • 1
    \$\begingroup\$ @Notbad the drag parameter maps to the "linear damping" value in the underlying Box2D physics engine, which you can find discussed in greater detail in previous Q&A \$\endgroup\$
    – DMGregory
    Oct 5, 2021 at 16:21
1
\$\begingroup\$

No, Unity does not offer you the option to apply different drag on different axis'. However what you can do is:

  • Implement your own drag mechanic, as in the answer by Acme Nerd Games
  • Change the drag coefficient in FixedUpdate depending on the current direction of the velocity vector. This is a solution I used once to implement a "drifting" mechanic where vehicles sliding sideways experienced higher drag than vehicles driving straight.
\$\endgroup\$
7
  • \$\begingroup\$ I have tried something like rigidBody.drag=HorizontalDrag; rigidBody.ApplyForce(Vector2(xForce,0)); rigidBody.drag=VerticalDrag; rigidBody.ApplyForce(Vector2(0,yForce)); but I did not get the expected results. I need to review my code because in this context there is a lot more code involved. But, just to be sure, Is this way of changing the drag property in FixedUpdate what you meant? \$\endgroup\$
    – Notbad
    Oct 5, 2021 at 20:06
  • \$\begingroup\$ @Notbad The reason why you don't get the results you want is because AddForce isn't affected by drag. The drag calculation happens during the physics update, which happens between two FixedUpdate's. \$\endgroup\$
    – Philipp
    Oct 5, 2021 at 20:55
  • \$\begingroup\$ Ah, I see. So to accomplish what I need, It would take 2 FixedUpdate "frames". One to update force along X axis and the other for the Y axis. I think this kind of delay could be very noticiable depending on the type of game. A platformer in my case. Perhaps in your scenario (the car game) this wasn't very important. \$\endgroup\$
    – Notbad
    Oct 5, 2021 at 22:37
  • \$\begingroup\$ @Notbad No, I don't think you understood this yet. Forces and drag are two completely separated mechanics. They both affect velocity, but they are not connected to each other. It does not matter for AddForce what drag value you have. \$\endgroup\$
    – Philipp
    Oct 6, 2021 at 7:55
  • \$\begingroup\$ Yes, I think I got it now. Anyway if I am not mistaken, this solution would not work for a platformer because I don't want the y component of the force be affected in any way when jumping. So, if user jumps and controls player on air, whatever drag I apply to the horizontal axis will be applied to the Vertical one. The problem here is that I don't have a continuous force space like in your car example. Here I just have Up (0,1), Left (1,0) and Right(0,1) and I just want to apply drag to the horizontal component. Never to the vertical one. By he way, thanks for your patience! \$\endgroup\$
    – Notbad
    Oct 6, 2021 at 8:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .