Godot Version
Godot Engine v4.4.1.stable.steam.49a5bc7b6
Question
I created a Node2D with a raycast2D and i want to use it more than one time in another scene, but when i add more than one of this node the raycasts conflict and it always use the first one. Does anyone know how to solve this problem?
The use of globals in this case isn’t helping you. How is Global.DashPanelForce
used? What do you expect to happen versus what really happens?
The Global.DashPanelForce was supossed to be used in the player script to change the player velocity
What @gertkeno said.
Based on your question, I’d say you either need to select Make Unique on a resource or you need to use duplicate() in your code. I can’t make heads or tails of your screenshots in regards to your question.
It would help if you posted your code in code blocks. Also, show us what Globals is doing.
Your Global.IsDashPanel
check seems unnecessary.
2 Likes
I’m guessing you intend to do that on-collision right? So why not use the collision data to detect which direction to go?
As an example something like this on the dash panel:
func _on_body_entered(body: Node2D) -> void:
if body is Player:
body.velocity += Direction.target_position.normalized() * DashPanelIntensity
Though I don’t know what your player script looks like so I can’t give specific advice until you show that.
2 Likes
The dash panel script
extends Node2D
@export var DashPanelIntensity: float = 1000
@onready var Direction = $Direction
func _ready() -> void:
pass
func _process(delta: float) -> void:
if Global.IsDashPanel:
Global.DashPanelForce = Direction.target_position.normalized() * DashPanelIntensity
print(Direction)
func _on_area_2d_body_entered(body: Node2D) -> void:
Global.IsDashPanel = true
func _on_area_2d_body_exited(body: Node2D) -> void:
Global.IsDashPanel = false
The global: script
extends Node2D
var DashPanelForce: Vector2
var IsDashPanel: bool = false
The Player script:
func _physics(delta):
if Global.IsDashPanel:
velocity = Global.DashPanelForce
Is $Direction your raycast? if so, the problem is your code is always referencing the same object.
And how do i change that?
Give me a sec, I’m writing some code for you.
1 Like
A couple things.
- I’ve renamed all your variables because the method you’re using, while it works is confusing because camel case is reserved for class names in GDScript.
- I implemented a signal to show you how you can pass information between objects “the Godot way”.
- I changed the Global class to just be a clearing house for a signal.
- The player’s velocity is now going to update every process frame if you want it to update every physics frame, you can move what function it’s in inside the DashPanel code.
- I added an export variable for you to attach the appropriate RayCast2D object to each instance of the DashPanel. This allows them to re-use the same code, but have their own instances of the ray cast.
- Keep in mind that velocity in a CharacterBody2D/CharacterBody3D can be updated anywhere in the code and then move_and_slide() will use whatever its current value is when it is called inside _physics_process().
dash_panel.gd
class_name DashPanel extends Node2D
@export var dash_panel_intensity: float = 1000
# You will drag-and-drop the appropriate raycast for each DashPanel into this field in the editor.
@export var direction: RayCast2D
@onready var active = false
func _process(delta: float) -> void:
if active:
Global.dash_panel_velocity.emit(direction.target_position.normalized() * dash_panel_intensity)
print(direction)
func _on_area_2d_body_entered(body: Node2D) -> void:
active = true
func _on_area_2d_body_exited(body: Node2D) -> void:
active = false
global.gd
#Assuming this is an Autoload with the name of `Global`
signal dash_panel_velocity(force: Vector2)
player.gd
class_name Player
func _ready() -> void:
Global.dash_panel_velocity.connect(_on_velocity_changed)
func _on_velocity_changed(force: Vector2) -> void:
velocity = force
1 Like
Okay so i tried Both your and @gertkeno solutions and both of them worked, i realized that the problem was that i used:
Global.dash_panel_velocity.emit(direction.target_position.normalized() * dash_panel_intensity)
Instead of:
Global.dash_panel_velocity.emit(get_global_transform().y.normalized() * dash_panel_intensity)
It worked perfectly when i used the get_global_transform()
1 Like