Welcome Back! Devs. In this blog I will explain how to implement an enemy bounce-off mechanism in a 2d game using unity. Let’s dive into it.
Enemy Bounce-Off Mechanic in Games
The enemy bounce-off mechanic is a classic feature in many platformer and action games. It allows the player character to bounce upward or away after jumping on an enemy. This mechanic serves multiple purposes, such as defeating enemies, navigating the level, or chaining movements for combo-based gameplay.
The satisfaction of timing a perfect jump and chaining bounces adds a layer of depth and flow to gameplay, making it a popular choice in game design. Often, bounce-offs are accompanied by sound effects, animations, or particle effects to enhance player feedback and immersion
Example games include:
Super Mario: Bounce off enemies like Goombas and Koopas to defeat them or gain height.
Hollow Knight: Use the nail to pogo off enemies and hazards for combat and traversal.
Shovel Knight: Perform shovel-bounces on enemies to chain attacks and navigate levels.
Sonic the Hedgehog: Maintain momentum by bouncing off enemies while speeding through levels.
Cuphead: Parry off pink objects and some enemies for movement and score bonuses.
Rayman Legends: Bounce off enemies to chain movements and maintain platforming flow.
Celeste: Bounce off certain elements to gain momentum and access challenging areas.
Player Controller Script
The current controls for my game’s demo character are:
Move - Left/Right
Jump
Dropdown (Only if an enemy is under the character).
So, the idea is to make the player bounce off the enemy only when the character is in the Dropdown action/state. If the player simply collides with the enemy object, nothing happens. How to achieve this? let us see!
Enemy Set up
For the enemy game object, I added two colliders:
Main Collider: Covers the enemy's body for general interactions.
Top Collider: Specifically detects when the player lands on top of the enemy.
The top collider helps us identify when the player is in the dropdown state. If the player lands on this collider, the enemy takes damage, and the player bounces off. Otherwise, no interaction occurs.
To make the enemy more dynamic, I also added a movement script that allows it to patrol between two points.
Mechanism
The dropdown action is implemented using RaycastHit2D and the gravity component.
A raycast is cast directly below the player to detect enemies.
If the ray detects an enemy and the player presses ‘S’ while in the detection range, we add a down force to the player using the AddForceY() and a float variable (Say downForce) that determines the amount of force that is applied on the player.
AddForce(-downForce, ForceMode2D.Impulse);
The negative sign is multiplied with the downForce variable to make the force apply towards the negative y-axis (Downwards). Make downForce public so you can easily tweak it in the inspector.
The bounce action is also implemented using the AddForceY() method.
Create a float variable, such as projectionForce, to determine the amount of force (Upward force) applied to the player.
When the player collides with the enemy’s top collider during the dropdown state, apply the force like this:
AddForceY(projectionForce, ForceMode2D.Impulse);
Just like the downForce variable, make projectionForce also public so that you can easily modify it in the inspector. This allows you to play with different values and lets you achieve your desired behavior (Bounce).
This method ensures the player gets a satisfying bounce effect when landing on the enemy.
Enhancing the Mechanic
To make the mechanic feel more polished, consider adding:
Particle Effects: Trigger particles upon impact for visual feedback.
Sound Effects: A bounce sound can greatly enhance immersion.
Camera Shake: Adds impact to the bounce.
Animations: Show the enemy reacting to being bounced on.
Conclusion
The enemy bounce-off mechanic is a fantastic feature for 2D platformers. It not only enhances gameplay responsiveness but also provides a rewarding experience for the player. While this is my method of implementing the mechanic, there are likely other ways to achieve the same result. Explore Unity’s libraries—you might discover alternative approaches!
If this blog felt a bit advanced, let me know in the comments, and I can write a beginner-friendly version. For the code base and art assets of this project, check out my GitHub—I’ll update it soon.
That’s it for this blog, Devs! If you have any questions or need further clarification, drop a comment below, and I’ll be happy to help. Keep developing and keep rocking, Devs! I’ll see you in the next blog.