UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

physics

[URP] Fix 2D Rocket Jump Horizontal Movement Constraints

Solution

physicsrigidbodyforcekinematics

Unity 2021.x - Unity 6.3.x

Published Sat, Mar 28

Issue

 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.

Explanation

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.

  1. Identify all locations where Rigidbody2D.velocity is manually assigned to handle horizontal input.
  2. Replace those assignments with AddForce using ForceMode2D.Force or ForceMode2D.Impulse depending on the desired responsiveness.
  3. Ensure all physics-related force applications, including those for character movement and the rocket jump, are performed within the FixedUpdate callback.

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 linearDrag on the Rigidbody2D to prevent the player from sliding indefinitely after a horizontal blast.
  • Use ForceMode2D.Impulse for 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.