How to make the sprite move to where the mouse is pointing (this is for over the top shooter)

Godot Version

Godot 4.5

Question

So can somebody explain why this script isnt working because even if my mouse is pointing left and i press W it just goes up and doesnt go the place where the mouse pointer was `extends CharacterBody2D

@export var speed = 400

func get_input():
rotation = get_global_mouse_position().angle_to_point(position)
velocity = Vector2(0, Input.get_axis(“up”, “down”)) * speed

func _physics_process(delta):
get_input()
move_and_slide()`

rotation doesn’t rotate the velocity. If you set the velocity to up, the body will go up no matter its rotation.

To rotate the rotation vector, this should work:

# instead of velocity = Vector2(0, Input.get_axis("up", "down")) * speed
velocity = Vector2(0, Input.get_axis("up", "down")).rotated(rotation) * speed
1 Like