0 votes

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
in Engine by (20 points)

1 Answer

0 votes

According to what I understood, you can use applycentralimpulse () 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
by (2,100 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.