Spring that pushes player

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Repertix

So, I’m using motion for player movement and I want to do a spring that pushes the player to a side

I tried using

body.motion.x += pushforce
body.motion = body.move_and_slide(motion,Vector2.UP)

It works, but it pushes instantly rather than in a smooth way, making it look like it was teleported. How it can be smooth?

:bust_in_silhouette: Reply From: Lopy

You can add a property in your player, say momentum: Vector2, and have springs add to it instead of moving the player directly.
Then, inside a _physics_process() you would move the player by momentum using move_and_slide, and then multiply momentum by 0.95 to reduce it progressively, simulating friction.

And how can I move it with momentum? I need to use move_toward?

Repertix | 2021-04-07 04:31