Godot Version 4.3
Hi I’m a newbie to programming, I am trying to create a movement system with a mouse. When the player left clicks the sprite moves and when the player left clicks again the sprite stops at the mouse’s position. How can I achieve this?
This is my current code that has the sprite following the mouse’s position:
@export var speed = 200
var mouse_position = null
func _physics_process(_delta):
velocity = Vector2(0,0)
mouse_position = get_global_mouse_position()
var direction = (mouse_position - position).normalized()
velocity = (direction * speed)
move_and_slide()
look_at(mouse_position)
You can get left mouse click by using _input method like this
func _input(event):
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
print("Left mouse button clicked")
Currently you are using mouse_position = get_global_mouse_position()
inside _physics_process, so that means that your mouse_position will update every physics frame.
You probbably want to move that under mouse click?
1 Like
Also, the mixture of global and local coordinates may cause problems.
2 Likes
I added your code after the physics process function. The game registers the left click’s input, but the sprite is not stopping at the mouse’s position
Did I input mouse_position = get_global_mouse_position() correctly?
func _input(event):
mouse_position = get_global_mouse_position()
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
print("Left mouse button clicked")
If I place mouse_position = get_global_mouse_position() under any other If statement listed in input function, I get an error in the direction variable in the physics process function
Did you remove
mouse_position = get_global_mouse_position()
from _physics_process?
Also move mouse_position under print like this:
func _input(event):
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
print("Left mouse button clicked")
mouse_position = get_global_mouse_position()
1 Like
I removed “mouse_position = get_global_mouse_position()” from the physics process. Here is what my current code looks like:
extends CharacterBody2D
@export var speed = 200
var mouse_position = null
func _physics_process(_delta):
velocity = Vector2(0,0)
var direction = (mouse_position - position).normalized()
velocity = (direction * speed)
move_and_slide()
look_at(mouse_position)
func _input(event):
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
print("Left mouse button clicked")
mouse_position = get_global_mouse_position()
When I run the game, the engine lists the error: “Invalid operands ‘Nil2’ and ‘Vector2’ in operator ‘-’” at the direction variable and does not run
That means that you are trying to update your CharacterBody2D on physics frame, but we didnt set mouse position.
Just add check did we set mouse position
func _physics_process(_delta):
if mouse_position:
velocity = Vector2(0,0)
var direction = (mouse_position - position).normalized()
velocity = (direction * speed)
move_and_slide()
look_at(mouse_position)
1 Like
I placed this code in and it works, however it wasn’t what I was trying to achieve. The sprite follows and stops at the point where the mouse clicked. The sprite doesn’t follow the cursor. What I’m trying to achieve is when the player left clicks the sprite moves and when the player left clicks again the sprite stops at the mouse’s position
Ok, try this
extends CharacterBody2D
@export var speed = 200
var mouse_position = null
var is_moving_enabled = false
func _physics_process(_delta):
if is_moving_enabled:
mouse_position = get_global_mouse_position()
var direction = (mouse_position - position).normalized()
velocity = (direction * speed)
move_and_slide()
look_at(mouse_position)
else:
velocity = Vector2(0,0)
func _input(event):
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
print("Left mouse button clicked")
is_moving_enabled = !is_moving_enabled
Clicking the mouse will toggle is_moving_enabled. When enabled, the CharacterBody2D will follow the mouse cursor.
Is this that you want to achive?
1 Like
Yes it is! Thank you so much!