Best way to stop an RigidBody2D

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

Main Script

func _on_player1_pressed():
      get_tree().call_group("Players1",  "next_level")

Player Script

func next_level():
      # here i want to stop the RigidBody2D's movement 

i tried many things like disable, stop, disconnect, linear_velocity, set_linear_velocity, …
maybe this is important

onready var velo = set_linear_velocity(Vector2(rand_range(200, -200),rand_range(200, -200)))
:bust_in_silhouette: Reply From: Millard

this is code I wrote for a previous question where the player was moved by clicking the mouse, but the slowing should still help you:

extends RigidBody2D

var speed = 50
var slow = 10
var deceleration = 25


func _physics_process(delta):
	var mouse_position = get_global_mouse_position()
	
	var sling_direction = mouse_position - self.position 
	
	if Input.is_action_just_released("slingshot"):
		apply_central_impulse(sling_direction * speed)
	#if Input.is_action_just_pressed("Slow"):
	#	apply_central_impulse(-linear_velocity * slow)

func _integrate_forces(state):
	if Input.is_action_pressed("Slow"):
		var new_speed = state.linear_velocity.length()
		new_speed -= deceleration
		if new_speed < 0:
			new_speed = 0
		state.linear_velocity = state.linear_velocity.normalized() * new_speed

_integrate_forces(state) is the part that will help probably. hope this helps you. =)

just a note, this stops the player gradually, which may or may not be what you want, but it should be able to be adjusted.