Can't figure out player controller

It seems that every single time I try to develop a 2D sidescrolling game, the biggest (and first) problem I encounter, is always the physics. This is mostly due to me needing very specific physics, and not ones that are commonly used.

I have tried using a rigidbody, and playing around with all the settings possible, but that just didn’t work in the slightest. I even tried using the code from Sebastian Lague, which for those who don’t know, is insanely well made, and is practically a whole physics engine on it’s own. But there are problems I have with how it works, and due to it’s complexity, I can’t take it apart and leave the stuff I need, without breaking the whole thing.

Here’s exactly what i need from my character controller:

  • Has to have solid/rigid/static movement, meaning no smoothing whatsoever (instantly rigidbody is not suitable).
  • It has to be able to climb slopes, and move on them at the same speed as on flat ground. And also be able to stay on them when nothing is being pressed (not slide down).

And so far, that seems to be all I need for the base of the controller. Yet it seems impossible to achieve with the inbuilt Unity physics engine, and therefore, without having a whole lot of code for a custom physics engine.

From my experience using built-in Unity physics, the CharacterController component would most likely be exactly what I need. But clearly there’s no “2D” version available in the Editor. I’ve seen some people talk about a 2D version being available, but I’m not sure how up-to-date that is, and how reliable it is.

Just a note, I am not an expert programmer. I wouldn’t even consider myself a “good” programmer. I can get around a lot of stuff, but I don’t have a single clue when it comes to actually making your own physics. And what I’m trying to achieve seems really basic (in the concept), but really complex (in the way physics work). So I’m honestly completely stuck.

Dude, I feel your pain. I was the same way back in the day. Wouldn’t you know it, sliding collision in itself is kind of a big deal if you’re doing it yourself… that’s why we use physics engines lol. If we play by the rules, it all works out and we can make games in no time! But if we try to fight the machine, things can get ugly fast.

So, what you’re asking for is not terribly much! But you do need to understand what’s going on at a basic level.

First of all, you don’t want your character to slide down a slope. You may want to look into the physics Materials being used and increase friction between your character and the floor! No coding required, just some editor setup :slight_smile:

Now, I’m not sure what you mean exactly by “Has to have solid/rigid/static movement, meaning no smoothing whatsoever (instantly rigidbody is not suitable).” But let’s not be naive here… we can’t always have our cake and eat it too. So, we make compromises from time to time to play nice with systems we don’t particularly care for. In this instance, you could have your character’s collider use the physics engine’s “not rigid” movement, and you will manually update the sprite’s location in a script to follow it at slowed down time intervals to give it the appearance of “staggered movement” while still being perfectly bound by the physics engine. You get to choose the shape of your collider without compromising your character sprite (boxes are good at not sliding down slopes), and you could even go so far with this script as to bind the character sprite to a “pixel grid” of size that is your choosing.
I’ll leave that up to you to figure out as an exercise in development. :smile:

To master the art of the workaround is part of being a developer. Don’t worry if you’re stumbling at first, we all had to do it. :wink:

1 Like