Player Move to Click script issues

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

Your code seems to work, I copied and pasted it and it works perfectly.
The issue may be related to something else, like maybe an offset on the sprite based on its parent, something like that?

EDIT: actually, I changed the input part to use if Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) as your input condition did not work, but the issue is surely not related.

yes thank you! It was offset a bit from the sprite.

1 Like

actually one more small issue and its weird It wont show up when I record it with OBS or my Phone. When making certain clicks the character gets stuck in a spammed state moving back and forth. Completely bizarre that it wont show up on video