Game Development
How to Make a Trampoline in Unity 2D Using Collision and Impulse
Create a working trampoline in Unity 2D using C#, collision detection, and Rigidbody2D. Learn how to apply an upward impulse, configure the components, and fix common mistakes.
A small green character lands on a red platform and jumps back into the air. There is no elaborate spring mechanism in the scene. The platform simply detects the collision and applies an upward impulse to the player’s Rigidbody2D.
That is the effect demonstrated in the accompanying Unity recording. It uses an existing 2D platformer scene, a script named Tramplin, a Box Collider 2D, and a configurable value called power. The recording was made in Unity 2020.1.17f1, though the same basic 2D physics approach is supported by current Unity versions.
The starting scene contains a character, floor, coins, and the small red platform that will act as the trampoline.
What the scene needs
You can follow the approach with an existing 2D platformer or a minimal scene of your own. The essential pieces are:
- A player object with a
Rigidbody2Dset to Dynamic and an appropriate 2D collider. - A platform with a
BoxCollider2D. Keep Is Trigger disabled for the collision callback used here. - The
Playertag assigned to the object that should bounce. - A script on the platform that reacts when the player makes contact.
The visual appearance of the platform does not affect its physics. The red rectangle in the recording is just a convenient way to identify the surface that launches the character upward.
Create the trampoline script
In the Project window, create a C# script named Tramplin. Open it in your code editor. The recording starts with the standard Unity script template before replacing it with collision logic.
The newly created Tramplin.cs file, still containing the default Start and Update methods.
We do not need Update() for this interaction. The bounce should happen when something makes contact with the platform, rather than being checked every rendered frame.
The relevant callback is OnCollisionEnter2D. Unity invokes it when a 2D collision begins and the involved objects meet the physics requirements.
Use this complete version of the script:
using UnityEngine;
public class Tramplin : MonoBehaviour
{
public float power = 10f;
private void OnCollisionEnter2D(Collision2D collision)
{
if (!collision.gameObject.CompareTag("Player"))
return;
Rigidbody2D playerBody = collision.gameObject.GetComponent<Rigidbody2D>();
if (playerBody == null)
return;
playerBody.AddForce(Vector2.up * power, ForceMode2D.Impulse);
}
}
The first condition checks whether the colliding object carries the Player tag. Without that check, any suitable rigidbody colliding with the platform could receive the same impulse. The second check avoids trying to apply force to an object without a Rigidbody2D component.
Vector2.up supplies the direction: the positive Y axis. Multiplying it by power changes the magnitude, while ForceMode2D.Impulse applies an immediate impulse instead of a continuous force over time.
The script is written around the collision callback and a check for the Player tag.
The impulse call being edited in the recording. This is an intermediate code frame; use the corrected, complete snippet above.
An impulse is appropriate for a trampoline because the character should respond at the moment of contact. The result still depends on the player’s mass, gravity, and incoming vertical velocity. A power of 10 will not produce an identical jump in every game.
Why the 2D types matter
There is a discrepancy between the recording and the separate Tramplin.txt file provided with it. The recording uses Collision2D and Rigidbody2D, whereas the text file mixes 2D and 3D physics types.
This declaration is not the correct signature for the collision callback:
void OnCollisionEnter2D(Collider2D collision)
For OnCollisionEnter2D, Unity expects Collision2D:
void OnCollisionEnter2D(Collision2D collision)
Collider2D is the parameter type for a different callback, OnTriggerEnter2D. The two callbacks are not interchangeable. A trigger detects overlap; the collision event here is used with a solid collider.
Likewise, the force is applied through Rigidbody2D, not the 3D Rigidbody.
Attach the script to the platform
Return to Unity. Select the red platform in the Scene or Hierarchy and attach the Tramplin script. You can drag the script from the Project window onto the platform or add it through the Inspector.
The recording shows the Tramplin (Script) component with Power = 10, together with a Box Collider 2D on the platform.
The selected red platform has the trampoline script, Power set to 10, and a non-trigger Box Collider 2D.
The platform does not need its own dynamic rigidbody merely to receive the collision callback in this setup. A stationary collider can interact with the player’s dynamic Rigidbody2D.
Check the collider bounds in the Scene view. If the collider is much larger than the visible red platform, the character may bounce before appearing to touch it. If it is too small, the character may miss the intended surface.
Set the Player tag
The script relies on CompareTag("Player"), so the player’s tag must match that string exactly.
Select the player object, open its Tag dropdown in the Inspector, and choose Player. If the tag does not exist in your project, create it first.
The recording specifically reminds the viewer to assign the Player tag to the character.
A common mistake is setting the tag on a parent object while the collider belongs to a differently tagged child. The exact hierarchy matters because the script checks the object reported by the collision. For this simple setup, keep the player’s relevant collider and Rigidbody2D on the tagged player object.
Test the bounce
Enter Play Mode and move the character onto the red surface. If the 2D physics components, tag, and script are configured correctly, the collision adds an upward impulse.
The game running in Play Mode, with the green character above the red trampoline surface.
If the character barely moves, inspect power, the player’s Rigidbody2D mass, gravity scale, and current movement logic. A movement script that continually overwrites vertical velocity can interfere with the effect. Increasing power is not always the correct fix.
If nothing happens at all, first check for compiler errors in the Console. Then verify that the platform is not marked Is Trigger, that both objects use 2D physics components, and that the player is tagged correctly.
There is another limitation to this particular implementation: it launches the player whenever a qualifying collision begins, including contact from the side. The recording demonstrates a simple bounce, not a platform that checks whether the character landed on its top surface. If your level requires that behavior, contact direction should be examined separately.
The useful part of this approach is its small size. There is no need to build a separate animation or complex state system merely to experiment with a spring platform. A collision event, a tagged player, and one impulse are enough to establish the mechanic. Once that works, the bounce can be refined to fit the game’s movement and level design.