[Rigidbody2D] Fix Post-Impulse Sliding for 2D Characters
Under Audit
Unity 2022.3.x - Unity 6.1.x
Published Tue, Mar 24
After implementing a rocket jump using Rigidbody2D.AddForce, characters may experience persistent sliding during horizontal movement, a common issue when balancing impulse-based mechanics with precise character control.
When implementing a rocket jump mechanic, issues with character movement can arise. Initially, a rocket jump might exclusively propel your character upwards, even when attempting diagonal propulsion. This issue may seem resolved by changing the impulse application from directly manipulating linearVelocity to using Rigidbody2D.AddForce with ForceMode2D.Impulse for the rocket jump. However, this often introduces a new highlight: your character slides excessively when attempting horizontal movement. This sliding occurs because Rigidbody2D.AddForce might not be the optimal method for continuous horizontal character control, especially when aiming for precise stopping without external friction. While applying a PhysicsMaterial2D with friction to ground colliders can mitigate this sliding, a code-based solution is often desired for more granular control over movement without relying on material properties.
- Implement a custom friction force in
FixedUpdateby settingRigidbody2D.dragor applying an opposing force when not receiving input. * Consider usingRigidbody2D.velocitydirectly for horizontal movement inFixedUpdateandRigidbody2D.AddForcefor instantaneous impulses like the rocket jump. * Ensure theRigidbody2D’sangularDragis set appropriately to prevent unwanted rotation contributing to sliding.
Editor's Note:
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.