2d games physics - best practice ?

Hello,
i’ve seen quite a lot 2D platform games made with unity, so i wonder if there is a ‘best practice’ to realize the physics there.

I’m working on a breakout clone right now, and had to ensure that no object moves on its y-axis (up or down) since the camera is watching the scene from the top.

my solution was, to check every frame and for every object, if it’s height value has changed, and teleport it back to zero then, and also, if it’s velocity.y has changed - and set it’s back to zero, also.

First try was to use an invisible roof cube, but then,
objects often got stuck.

Is the idea of manually zeroing an axis every frame a good one, or do all the plattform game developers a perhaps much easier/more performant way of implementing 2d physics ?

And IF it is the best solution: What would be the most performant way of doing it?

all the best,
David

Check this out Unity 2D tools for game dev – evolved for optimal graphics performance

I am starting work on a 2D side scroller myself and that tutorial is very informative and should be very useful to you. As for manually resetting the object’s Y location each frame, that probably isn’t your best option. I tried that before and I got some weird results with physics. What I see used in that tutorial to restrict movement is that they use a Configurable Joint.

To add a configurable joint to an object, select it in the scene and click Component>Physics>Configurable Joint. From there you can restrict many types of movement such as linear and angular movement. Go ahead and set the Y motion to Locked. That should keep it locked on the XZ-plane. Hope this helps you.

Hi,

thank you for your reply.
That helped very much. I will compare it to he performance with my method, but for realistic looking effects, it will be the method of choice in any case.

That was quite useful, i didn’t know there was a special joint which isn’t really used as joint but as delimiter.

Kind Regards,
David