My RayCast2D node isn't detecting collisions with objects that should be in its path. What could be causing this?"

I want to make a Bubble Shooter style work with Godot Engine 4.3 and I need to draw a virtual line during launch. First, I created the RayCast2D node and added 2 Line2Ds as “LineOne and LineTwo” as its child nodes. Then, when I targeted this virtual line to objects, I wanted the objects to get the color I assigned to their IDs. Now the things I targeted with the code I gave below are working correctly, but the virtual line “LineTwo” is working, but this code cannot get the color of
the targeted object with “di.default_color = get_collider.color” and also “dl.points|11.x =
vector_line_length” with this code, the virtual line does not end after reaching the targeted object.
These two lines of code are not working in my project. How can I solve this correctly? Thank you for your help.
Screenshot:

My Codes:
"

extends RayCast2D
@onready var vl = $LineOne
@onready var dl = $LineTwo
var vector_line_length = 600
@onready var manager = $"../Manager"
var default_color: Color = Color(0.388,0.643,0.969,1)

func _physics_process(_delta: float) -> void:
	
	if not $"../Shoot”.was_shooted:

		vl.visible = true
		dl.visible = true

		if $"../Shoot”.can_be_shot:
			look_at(get_global_mouse_position())

		if is_colliding():
			if not manager.items.has(get_collider()):
				var collision_point = to_local(get_collision_point())
				vl.default_color = default_color
				dl.default_color = default_color
				vl.points[1].x = collision_point.x
				dl.position = collision_point
				dl.rotation_degrees = (rotation_degrees * 2) * -1
				dl.visible = true
			else:
				var collision_point = to_local(get_collision_point())
				if get_collider().color != null:
					vl.default_color = get_collider().color
					dl.default_color = get_collider().color
				else:
					vl.default_color = default_color
					dl.default_color = default_color
				vl.points[1].x = collision_point.x
				dl.visible = false
		else:
			vl.default_color = default_color
			vl.points[1].x = vector_line_length
			dl.points[1].x = vector_line_length
			
	else:
		vl.visible = false
		dl.visible = false

"

1 Like

I’m not sure what you mean by “I wanted the objects to get the color I assigned to their IDs”. How and where did you assign the colors?

I created each object in different scenes and assigned ID and color to them using the StaticBody2D node in those scenes to ensure that each object appears randomly on the screen and at which level the colors I want.
The LineOne is working correctly and if you look at the code you will notice it, but the LineTwo is not working and I need help here.

1 Like

I’m sorry. Perhaps I need more context, but I’m not sure what the code is supposed to be doing.

What is the purpose of the two lines, why are there two of them, and what is different about them? They share a single RayCast2D and collision hit, correct?

Maybe you can add some comments that explain what certain sections of your code are responsible for and/or a screenshot to better illustrate the problem?

2 Likes

Consider a project in Bubble Shooter style and I am targeting objects from below. I want a virtual line to form during this targeting and I made this virtual line with 2 Line2Ds because I could not find a solution with 1 Line2D. These operations I did actually work correctly, but the codes I specified in the question are not reflected in the second line. I will give visual examples below. Thanks for your help.

1 Like

Ah ok, now I understand. I think the problem is that you are trying to use the same RayCast for both lines. The second line has no colour and doesn’t stop at the circle it hits becaue there is no RayCast to tell it there was a hit.

You can either use multiple RayCast nodes (one for each line) or you can reposition the RayCast and force it to update using forceRaycastUpdate (see the docs on usage and restrictions RayCast2D — Godot Engine (4.0) documentation in English). The forceRaycastUpdate approach might be easier to fit into your current code, just make sure to set the raycast back to its original position afterwards if you do.

Another option is to make raycasts against the PhysicsDirectSpaceState2D, but this works a little differently. Ray-casting — Godot Engine (4.0) documentation in English

I’ll try this. Thank you.

1 Like

I tried what you said but I didn’t get any solution. What change should I make in the codes?

How does your code look now? Which solution(s) did you attempt?