2D: Can You Detect Sliding ?

Hi, I have a 2D game where the surface that the player stands on with rotate left and right creating a slope. At a certain point the player sprite will start to slide due to gravity/mass/etc.

Is there a way to detect if the player is moving due to a slide? for example, If i want to play a sound of shoes scrapping across a stone floor when the player is sliding.

Maybe I can answer my own question. Is it a matter of checking for velocity > 0 when the directional keys are no being pressed? Or is there a Unity Function something like “OnRigidBody2DSlide()”

Thanks!

No there is no such function to detect sliding. You can do it using few condition checks:

  • See if the player and the platform are in collision with each other. Use OnCollisionStay for this.
  • Check if your platform is either tilted towards left or right by checking its rotation.
  • Now check if the velocity of the player is greater than zero.

If all of it is true then the player is sliding.