Godot 4.3 ( 2D)
Hello. I am very new and I am sorry to ask but I looked everywhere for a solution and I am lost.
My issue is: I have 4 characters, I need to be able to select 1 of them and move it while the other 3 stay in 1 place until I click on them and request them to move.
I have 4 different tabs - for every character. Each one has this script:
extends CharacterBody2D
var speed = 300
var click_position = Vector2()
var target_position = Vector2()
var can_move = true
func _ready():
click_position = position
func _physics_process(delta):
if Input.is_action_just_pressed(“left_click”):
click_position = get_global_mouse_position()
if can_move:
if position.distance_to(click_position) > 3:
target_position = (click_position - position).normalized()
velocity = target_position * speed
move_and_slide()
func _on_selection_area_2d_selection_toggled(selection: Variant) → void:
can_move = selection
# …
pass
Each one has a Area2D with a signal to selection_toggled and a script:
extends Area2D
signal selection_toggled(selection)
@export var exclusive = true
@export var selection_action = “select”
var selected = false
func set_selected(selection):
if selection:
_make_exclusive()
add_to_group(“selected”)
else:
remove_from_group(“selected”)
selected = selection
emit_signal(“selection_toggled”, selected)
func _make_exclusive():
if not exclusive:
return
get_tree().call_group(“selected”, “set_selected”, false)
func _input_event(viewport: Viewport, event: InputEvent, shape_idx: int) → void:
if event.is_action_pressed(selection_action):
set_selected(not selected)
If there is anyone that can help me with an explanation for a newbie I would be forever grateful.