2D platformer player movement

I know this topic is probably already talked about a lot here, but I just found a lot of conflicting information on this.I saw people using AddForce(), others using rigidbody.velocity, others saying that the physics in Unity are terrible and I should do my own “custom” physics… And I just know what to do anymore.

So, what advantages each approach has and what is recommended for an average programmer? All I want is my character to slide and accelerate easily, but still be affected by friction while not sticking to walls, and to not gain speed infinitely (which is what happens when you use AddForce() by itself).

(BTW I am only talking about moving left and right, I will handle jumping, crouching, sprinting and etc later).

Moving objects is a lot more complicated than it seems. All of these methods are valid, and in fact they all overlap with each other.

For example, setting rigidbody.velocity = someValue and AddForce are ultimately the exact same thing. Depending on the ForceMode you use, you can emulate calling AddForce by just setting velocity as follows:
Force is like: rb.velocity += (force * Time.fixedDeltaTime) / rb.mass;
Impulse is like: rb.velocity += force / rb.mass;
Acceleration is like: rb.velocity += force * Time.fixedDeltaTime;
VelocityChange is like: rb.velocity += force;

So really, setting rigidbody.velocity and calling AddForce are exactly the same thing.

As for doing your own custom physics - well, maybe. PhysX is designed to emulate “real life” Newtonian physics with rigid objects pretty closely, and it does quite a good job at that. But real life newtownian physics don’t always work well for gameplay.

So all of this might not be want you want to hear, but the answer is… it depends. It depends on what kind of game feel you’re going for and it depends on how you want to approach the math.

Things like “not gaining speed infinitely” are easy to resolve with Unity’s physics engine if you just think about it a little bit. There are things like Vector3.ClampMagnitude which can easily be applied to the velocity to limit it. Or you can model drag or friction forces, either with the built in physics materials and drag settings on the Rigidbody or by modelling it yourself with AddForce in code. Then there are even more exotic things you can do if you’ve got the vector math skills, like remove the component of force you want to add that’s in the same direction as motion if you’ve reached the max velocity.

The possibilities are kind of endless, but it’s definitely not simple. And if it was simple then probably every Unity game out there would have similar movement characteristics and feel pretty “samey” and nobody wants that now do they? I think the reason information seems “conflicting” to you is that you’re looking for some “one right way” to do things and there simply is no one right way to do things. There are lots of different ways that work differently and end up with somewhat different results, and lots of different ways to approach things that end up with similar results, and everything in between.

2 Likes

I didn’t think about that actually, makes sense and it’s pretty obvious but I just never stopped to think about it.

Yeah I stayed a few hours thinking about a solution but ultimately it didn’t feel good enough, and the code seemed messy… I just knew there was something wrong with my approach, but couldn’t pinpoint what it was. I regret deleting it entirely, as I could post it here to help give some context of what I am trying to do. I will try these methods.

I am well aware of that, which is why I came here. If there was a simple solution for everyone I would have found it and implemented it in less than an hour. I want the help to understand what options are available and for what purpose each option works best, to then choose which one I want for my game.