Godot Version
4.3
Question
Im having trouble making a script to move the CharacterBody2D where I clicked on the screen.
extends CharacterBody2D
var speed = 300
var click_position = Vector2()
var target_position = Vector2()
func _ready():
click_position = position
func _physics_process(delta):
if Input.is_action_just_pressed("right_click"):
click_position = get_global_mouse_position()
if position.distance_to(click_position) > 3:
var direction = (click_position - position).normalized()
velocity = direction * speed
else:
velocity = Vector2.ZERO
move_and_slide()
In the image I circle where I click and the sprite is where it lands