2

I was trying to add a omnidirectional dash to my game with this code

if Input.is_action_pressed("ui_accept"):    
    velocity = Vector2.ZERO

    velocity = position.direction_to(get_global_mouse_position()) * 200

And it is only moving the player up and down

I expected it to move based on the mouse's position, it does but only on the y axis, not the x. If I add move_and_slide() to the bottom, it moves along the x axis but not smoothly as it moves abruptly like a teleport.

1 Answers1

1

I don't know why you get the behavior you describe. However, I can point to a possible issue with the code: It mixes local an global spaces.

In this expression:

position.direction_to(get_global_mouse_position())

The position is relative to the parent Node. Meanwhile get_global_mouse_position is global. So use global_position instead:

global_position.direction_to(get_global_mouse_position())

The alternative would be to convert get_global_mouse_position() to local space with to_local, but velocity should be in global anyway (and it is not a position, so do not convert velocity with to_global either).

Theraot
  • 31,890
  • 5
  • 57
  • 86