Trampolines, springboards, launchers, or "thingies that launch you into the air"- whatever they are called, they are easy to implement in Chipmunk2D!
You can download this tutorial and all the project files by getting Chipmunk2D for Unity on our store and download page.
Using a collision to start an event is an essential operation, and an great use of callbacks in Chipmunk. Check out the One Way Platform guide to learn about callbacks. This guide continues where that one left off.
Place this code inside a ChipmunkCollisionManager class, and collision callback methods will be called automatically. Let's break down each part of this.
First, we grab the separate player and mushroom shapes. As with PhysX, both colliding shapes are available. The important thing to remember is that arbiter.GetShapes gives you the shapes in the order that they are specified in the function name, ChipmunkBegin_player_jumpshroom.
As with the one-way platform, check the normal. This ensures the player is only launched upwards if they touching the trampoline from the top.
As with the one-way platform, check the normal. This ensures the player is only launched upwards if they touch the trampoline from the top.
In this situation, we're setting the y component of the velocity directly. This results in a completely consistent trampoline jump height. body.ApplyImpulse() would also work, but since the player might be hitting the trampoline with some residual y velocity, the jump might be higher or lower than needed for the level.
Finally, returning false from a ChipmunkBegin causes the collision to be ignored. In this situation it's not really an issue, but if you're making some sort of a launcher where the player could conceivably strike part of the launcher after being launched, that problem will be avoided.