0
\$\begingroup\$

I have seen a lot of people think that for a 2D platformer game, Unity2D physics is not really the best approach, and recommend implementing your own physics.

I found the idea interesting, but I don't know how to approach the issue.

Using 2D Rigidbodies, and setting them as kinematic, allows you to ignore unity's physics. But how would collisions be handled without relying on Unity collision system?

\$\endgroup\$
3
  • 2
    \$\begingroup\$ It's largely a matter of opinion & personal style whether to use the built-in physics. Myself, I like to use it because it gives me basic collision response "for free" so my stuff doesn't fall through floors & walls, and the built-in integrator is likely higher quality than anything I'd write, without overcomplicating my scripts with these details. Then, if I want to customize things like acceleration curves & jump arcs & bouncing, I do the math and apply my own forces/impulses to shape the physics simulation to what I want, rather than completely re-implement it. \$\endgroup\$
    – DMGregory
    Jan 30, 2018 at 17:06
  • \$\begingroup\$ I also consider using the built-in physics a better approach unless you want to do something extremely specific. Yet the idea of using a custom implementation was interesting, yet I can't wrap my head around it to actually make an implementation. And knowing how to do different things with your tool usually gives you ideas and better understanding of the engine you are working with. \$\endgroup\$
    – Leo
    Jan 30, 2018 at 18:05
  • \$\begingroup\$ You will need colliders for your level collisions, but you can remove the collider from your characters and replace it with raycasts. Realistic gravity/acceleration in 2d platformers often makes character movement floaty/unresponsive. As for colliders: The reason behind using raycasts instead is that sometimes even if 2 colliders (say two ground tiles) are perfectly aligned, due to float rounding a rectangular character collider can stuck when passing over them. Some replace with capsules but that leads to slipping off the edge of platforms. Today that stucking is solved by Unity's Tilemap. \$\endgroup\$
    – Nikaas
    Jan 30, 2018 at 22:53

2 Answers 2

3
\$\begingroup\$

The collision detection system doesn't require objects to have non-kinematic rigidbodies. You can set all rigidbodies to kinematic and still detect collisions. All you need are Colliders with appropriate shapes.

You can execute code when the collider of a game object intersects the collider of a different game object by setting the "is Trigger" flag on its collider and implementing the OnTriggerEnter2D, OnTriggerStay2D and/or OnTriggerExit2D events in one of its scripts. This allows you to implement mechanics like contact damage or projectile impacts.

Objects won't block each other's movement anymore, but if you don't want to use Unity's physics engine, then making that happen is your responsibility. Before you move an object, check if there are any obstacles in the way. The static methods for collision detection of the class Physics2D can help you to do that.

\$\endgroup\$
2
  • \$\begingroup\$ Using triggers was my first idea, but relying just on them seemed like a mess and convoluted in the long run if you want to make objects block each other. \$\endgroup\$
    – Leo
    Jan 30, 2018 at 18:07
  • \$\begingroup\$ @Leo Doesn't need to be. Just assign the same physics layer to all blocking objects. Then create a helper method MoveIfPossible(GameObject object, Vector2 destination) which does a BoxCast with a box in the size of the object's collider towards the destination to check if there is anything in the way. Use that helper-method whenever you want to move something which is subject to blocking behavior. \$\endgroup\$
    – Philipp
    Jan 30, 2018 at 18:21
1
\$\begingroup\$

How old were these peoples thoughts? And why do they think that?

Perhaps in the past, but now that Unity has a tilemap system (released October 2017), I don't see why you wouldn't use Unity2D physics to do this. This is the kind of thing that it is intended for, after all.

A tilemap with a collider attached, as well as a gameobject with a box collider and controller script, and you have a simple platformer in minutes. And if you want to add detail layers, you can just add another tilemap to the grid object, and not attach a collider.

I've also seen a collider merger that can be attached (though not used it myself yet) and that you can create animated tiles as well, as well as tiles that change depending on the tiles around them.

\$\endgroup\$
5
  • \$\begingroup\$ The reason why you wouldn't use real physics is because the jumping physics players expect from platformers usually defy real-world physics. \$\endgroup\$
    – Philipp
    Jan 30, 2018 at 16:39
  • \$\begingroup\$ Ah yes. I did in fact read something about that today, but unfortunately cannot recall where. Pretty sure there was a solution as well. That's going to irritate me now. \$\endgroup\$ Jan 30, 2018 at 16:48
  • \$\begingroup\$ Using the built-in physics engine doesn't require you to have real-world physics. It's entirely possible to implement arbitrarily exaggerated/customized physics responses by layering your own forces/velocities with the baseline inertial physics & collision response you get from the engine. \$\endgroup\$
    – DMGregory
    Jan 30, 2018 at 17:01
  • \$\begingroup\$ @DMGregory It just seems more easy to use your own velocity/acceleration/gravity/etc than use your own on top of the engine one. \$\endgroup\$
    – Nikaas
    Jan 30, 2018 at 22:27
  • \$\begingroup\$ @Nikaas this can vary. If you need to customize only certain objects' physics (eg. players & enemies but not crates) then you can save some work outright by only layering customizations where you need them. You can also often let the built-in physics integrator & collision resolution handle the bulk of the work. But there are games that need something so custom that trying to apply it as a layer over an existing simulation would require more complexity to fight with the built-in behaviour than to just implement it all from a blank slate. The trick is knowing whether that describes our game. ;) \$\endgroup\$
    – DMGregory
    Jan 30, 2018 at 22:58

You must log in to answer this question.

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