[URP] Fix 2D Rocket Jump Horizontal Movement Constraints
Solution
Unity 2021.x - Unity 6.3.x
Published Sat, Mar 28
A common issue encountered when implementing a rocket jump mechanic in Unity 2D is the inability to achieve diagonal or sideways trajectory. Despite applying an explosion force, characters are observed to only move vertically upwards, preventing the intended multi-directional displacement.
The primary cause of the restricted rocket jump movement is the direct manipulation of the Rigidbody2D.velocity property within your movement script. When an external horizontal force is applied, subsequent velocity assignments in methods responsible for general character movement (e.g., Update or LateUpdate) can overwrite the imparted horizontal velocity. This effectively nullifies any sideways or diagonal push from the explosion, leading to the character only exhibiting vertical movement because the upward force is not immediately countered by other velocity assignments.
To resolve this, the method of applying character movement within your movement script should be adjusted. Instead of directly setting Rigidbody2D.velocity for horizontal movement, it is recommended to consistently use AddForce. This approach allows external forces, such as those from a rocket jump, to accumulate and interact correctly with other movement inputs without being instantaneously overridden. Specifically, any lines within your movement script that directly assign values to Rigidbody2D.velocity for horizontal movement should be replaced with calls to AddForce.
- Identify all locations where
Rigidbody2D.velocityis manually assigned to handle horizontal input. - Replace those assignments with AddForce using
ForceMode2D.ForceorForceMode2D.Impulsedepending on the desired responsiveness. - Ensure all physics-related force applications, including those for character movement and the rocket jump, are performed within the
FixedUpdatecallback.
Additional Tips
- If you prefer the ‘snappy’ feel of direct velocity, use a boolean flag to temporarily stop manual velocity overriding while the character is in a ‘launched’ state.
- Increase the
linearDragon theRigidbody2Dto prevent the player from sliding indefinitely after a horizontal blast. - Use
ForceMode2D.Impulsefor the explosion itself to ensure the momentum change is instantaneous.
TL;DR
To enable diagonal or sideways movement for a rocket jump, ensure that direct assignment to Rigidbody2D.velocity is replaced with AddForce within FixedUpdate in your movement script. This prevents horizontal velocity from being overwritten.
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
Content inspired by a Unity discussion post.