How do I add a boosting behavior to my node's seeking behavior?

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

I would like to add some kind of a boost feature to my rigidbody2d node’s seeking behavior when the node gets closer TOWARDS the mouse position when under certain distance.

What I tried doing was adding on to the velocity of the node when it was close to the mouse position by calculating the distance, but that added to the velocity even when the the node moved past the mouse and AWAY from the mouse because the distance between the mouse and the node was still less than the specified distance. It made gradually change in velocity awkward unfortunately.

If there is a good way to add a temporary “boost” to a Rigidbody2D please let me know!

# seek() is called here    
func _integrate_forces(state):
    	apply_central_impulse(seek().clamped(max_force))

# Function to seek player
func seek():
	var player_loc = get_global_mouse_position() # Test variable to get mouse's position
	
	# Get the desired velocity towards the player
	var desired = (player_loc - get_global_position()).normalized()
	desired *= (max_speed)
	 
	var steer = desired - get_linear_velocity()
	
	return steer
:bust_in_silhouette: Reply From: estebanmolca

According to what I understood, you can use apply_central_impulse () or apply_impulse. Something like that:

extends RigidBody2D
var temp=true
func _ready():
	pass
func _physics_process(delta):
	var pos=self.position
	var mpos=get_global_mouse_position()
	if pos.distance_to(mpos) < 50 and temp:
		self.linear_damp=5
		self.apply_central_impulse((pos-mpos).normalized() * 350)
		temp=false
	if pos.distance_to(mpos) > 50:
		temp=true
	

#EDIT:
I forgot that for rigidbody another process function is used, it would be something like that but I don’t know if it’s the behavior you expect:

extends RigidBody2D
var max_force=150
var max_speed=150
var temp=true

# seek() is called here    
func _integrate_forces(state):
	linear_damp=2
	apply_central_impulse(seek().clamped(max_force))



# Function to seek player
func seek():
	var player_loc = get_global_mouse_position() # Test variable to get mouse's position
	var steer=Vector2(0,0)
	# Get the desired velocity towards the player
	var desired = (player_loc - get_global_position()).normalized()
	desired *= (max_speed)
	if self.position.distance_to(player_loc) < 100 and temp:
		steer = desired - get_linear_velocity()
		temp= false
	if self.position.distance_to(player_loc) > 100:
		temp=true	

	return steer