Jump to content
  • Advertisement
3dmodelerguy

Algorithm Raycasts or Rigidbody2D for 2D Platformer in Unity

Recommended Posts

So I am trying to prototype a 2D platformer like game in Unity and trying to figure out the best method that I should be investigating for controlling the player character. I see generally 2 different ways to handle this which is either using Rigidbody2D or using raycasts. The list of things that I am looking to do right now are:

  • has some sort of gravity like effect
  • moving side to side
  • jumping
  • double jumping
  • being able to walking up slopes of a certain angle (but prevent it after a certain angle and have the user slide down if they are on that angle or larger)
  • be able to hang off the edge and pull up

I sure some things might come up later in development if I get that far but these are what I want to get working at least at a very basic level before I move on from the character controller for the prototype (if you watch this video for about 15 - 20 seconds, you will see generally most of the functionality I am looking to achieve: https://www.youtube.com/watch?v=6rzTnx6IHqM&start=375).

Now I started with the Rigidbody2D since I do want to have some level of gravity in the game (not just the player but items, enemy, etc.) and I can get the first 4 working pretty easy but the 5th is more troublesome (and have not gotten to the 6th). I can move up angles (I am setting Rigidbody2D.velocity for movement as it seems to be the most recommended) but for example if I am walking up an angle and then stop, my character jumps up a little (I am guess because of the forward velocity that is has when I stop applying hortizontal velocity for moving side to side, there is still extra vertical velocity).

So I have 2 questions:

  1. Should I be looking at Rigidbody2D or manual raycasts?
  2. Either way that you recommend going, do you have any resources that you would recommend looking at for reference (video tutorials, articles, etc.)?

Share this post


Link to post
Share on other sites
Advertisement

Since no one's replied yet, I'll go ahead and mention a couple things.

It looks like the game you linked to might use Unity. Maybe the developers of that game have made information available about how they went about it. I imagine other people have tackled this in Unity as well, in which case there might also be something available in the Unity Asset Store, on the Unity forums, or elsewhere. (Maybe you've already pursued all these avenues.)

When you mention using raycasts, do you have a specific approach in mind?

The Unity physics system and raycasts may not be the only options. For example, another option would be a bespoke system. Although implementing such a system would likely be non-trivial (and may not be something you'd be interested in), it would allow you complete control, which could help address unwanted behaviors like the 'jumping' you described, and make custom behaviors like ledge-climbing more practical. With a physics system like Unity, I'm guessing you may have to fight it at times, and that you'd probably have to override it kinematically for ledge-climbing. (Of course if the game you linked to uses the Unity physics system, that proves that what you want to do can be done with said system.)

If I were doing something like this I'd probably try implementing a bespoke system first, as I think having that level of control could be useful. But, YMMV, and I realize there may be other factors involved (for example, you may also want to support more complex physics-based interactions that would be difficult to implement from scratch).

Share this post


Link to post
Share on other sites

I've done a few platformers with a friend using Unity 3D now. At the time we wrote our first platformer we used raycasting to handle the work of detecting walls and slopes and have used the same code, with tweaking, ever since. The system is a bit finicky to set up but works well after the initial work is done. You shouldn't have too much trouble getting most of what you want out of either option though edge grabbing might be a bit of a challenge compared to the other features.

IIRC there were a couple reasons we went with raycasting since we built the system in 2015 a lot is very much an "at the time" thing and might be incorrect or outdated thanks to updates to Unity.

  • I don't think Rigidbody2D existed at the time but I could be misremembering
  • We couldn't find a way of having one hitbox for detecting walls and another for different important interactions that we were happy with.
  • Even when we constrained rotation on all axes we still got slight rotations sometimes
  • Getting a good control feel was extremely difficult. IIRC we kept getting weird issues (sliding, getting stuck, jitter etc. etc) that would crop up randomly which raycasts didn't have

The way we did things was to have three rays (top, middle, bottom) cast for horizontal movement and three rays (left, middle, right) cast for vertical movement. Any intersection that's within the character's hitbox will result in the character being pushed back/up/down to eliminate the overlap. When embedded in a floor the player is always pushed up so it's impossible to get trapped though it does open the game to a class of exploit normally called a "zip" by speedrunners. Tweaking the locations of the bottom and left/right rays is used to control what sort of slopes that player can and cannot climb.

Unfortunately I don't know of any good tutorials for setting up something like our system since we only really used the Unity manual, pencil and paper and hacking to implement our platforming system. I could write some blog posts or something on how our system works if there's interest. I don't think a forum post would be the best place for going into all the details on how we made our stuff work.

 

Edited by Dan DAMAN

Share this post


Link to post
Share on other sites

Thanks for the advice. For the time being I decided to go with a premade solution (https://www.assetstore.unity3d.com/en/#!/content/26617). I don't have a deep enough knowledge of the math that would be required building that kind of system myself nor do I have the desire to push myself to learn that kind of math right now (this just being a side project, I would rather focus of the areas of game development that are more interesting to me where I don't mind using my free time at doing). The library has everything I would absolutely need and most of what I would ideally want. If this prototype gets further than most of my other prototypes I might invest more time into the movement aspect of the game myself (and I will already have a large set of script to be able to use as reference now) but I think trying to focus on that now before I have a bunch of the game play functionality working at a basic level would be premature for me.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!