I made a 2d shooter, and gave my player this recoil effect whenever I shoot. The player is moved slightly based on the mouse cursor. However, the movement is way to sudden. I want to smoothen it a little bit to make it more realistic. But I don’t know how. It’s one of the first times working with Vector2s this way(normally, I would do speed * 0.9 or something like that), so help is highly appreciated.
func move_player_away_from_cursor(length):
var player_position = global_position
var mouse_position = get_global_mouse_position()
var direction_to_mouse = (mouse_position - player_position).normalized()
var move_away_vector = -direction_to_mouse * length # Adjust the multiplier as needed
# Update the player's position
global_position += move_away_vector
You could use a tween, though it will be very rigid. is your player a physics body like CharacterBody2D? Try adding the kick back to velocity, and avoid outright setting velocity elsewhere; instead using move_toward when possible.
My mistake, there is a second float value called weight. The weight to set the interpolation.
Adding a booleab to determine when it should be called is exactly what you should do. I also want to assume you’re calling the move_player_away_from_cursor function from physics process right?