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

[Rigidbody2D] Fix Post-Impulse Sliding for 2D Characters

Under Audit

rigidbodyphysics2d development

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.

Issue

 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.

Experimental Fixes
  • Implement a custom friction force in FixedUpdate by setting Rigidbody2D.drag or applying an opposing force when not receiving input. * Consider using Rigidbody2D.velocity directly for horizontal movement in FixedUpdate and Rigidbody2D.AddForce for instantaneous impulses like the rocket jump. * Ensure the Rigidbody2D’s angularDrag is set appropriately to prevent unwanted rotation contributing to sliding.

Editor's Note:

The above fixes have not been verified by our audit team yet. They are provided exclusively for your own technical research. We recommend creating a backup of your project before proceeding with any attempts. Utilize at your own discretion!

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.