Godot Version
Godot v4.3.stable.arch_linux
Question
I’m currently making a little game about dodging asteroids, I used an Area2D node to detect when the mouse cursor is inside that area to stop the player (a CharacterBody2D) from moving (because it automatically moves while following the mouse cursor and without that, the player would uncontrollably shake)
The scene tree is as follows:
With the following code:
extends CharacterBody2D
@export var speed = 500
var point_to_mouse = true
func kill():
pass
func _ready():
global_position = get_viewport().size / 2 #Asignar la posision global de la nave al centro de la pantalla
$Mouse_Detector.mouse_entered.connect(_on_mouse_detector_mouse_entered)
$Mouse_Detector.mouse_exited.connect(_on_mouse_detector_mouse_exited)
func _physics_process(delta):
##MOVIMIENTO##
#var direction = Vector2(Input.get_axis("Down_Movement","Up_Movement"), 0).rotated(rotation)
var direction = Vector2.RIGHT.rotated(rotation)
velocity = direction * speed
#Parar el movimiento cuando el cursor este en el centro del jugador
if !point_to_mouse:
velocity = Vector2(0,0)
else:
look_at(get_global_mouse_position()) #Rotar la nave para que apunte hacia el cursor
move_and_slide()
#Checar si el mouse esta o no esta en el area del "mouse detector" y cambiar la variable dependiendo del caso
func _on_mouse_detector_mouse_entered() -> void:
print("Mouse entro")
point_to_mouse = false
func _on_mouse_detector_mouse_exited() -> void:
print("Mouse salio")
point_to_mouse = true
The code works perfectly when I’m playtesting in the “Player” scene, but when I’m playtesting in the “Main_Scene” Scene, the code doesn’t work, as if the signals somehow were not being sent.
The scene tree of the “Main_Scene” is as follows:
With this code:
extends Node2D
func spawn_asteroids():
var new_asteroid = preload("res://Scenes/Objects/Asteroid.tscn").instantiate()
$Path2D/PathFollow2D.progress_ratio = randf()
new_asteroid.global_position = $Path2D/PathFollow2D.global_position
add_child(new_asteroid)
new_asteroid.look_at($Player.position)
func ready():
spawn_asteroids()
func _on_timer_timeout():
spawn_asteroids()
As I said, when I playtest in the “Main_Scene” Scene, the code doesn’t work, as if the signals weren’t connecting, but when I playtest on the Player Scene, it works perfectly. Is there a way to fix this issue? Thanks in advance.
PD: I was going to add videos to show the spaceship in the “Player” Scene, and another one in the “Main_Scene”, but the forums wouldn’t let me because I made my account recently
PD02: I am not a native English speaker (I speak Spanish if you didn’t notice the comments), and I was using Grammarly while writing this, so I’m deeply sorry for any misspelt word or not understandable sentence