xyz The reason I use RayCast2D as the main node is because the codes I use give errors in other nodes. That's why I used this one. Which node shape should be the right one?
How can I activate Line2D, the reflection line with RayCast2D?
- Edited
codernesim Node2D should be sufficient in this case. Or just add one additional ray cast to it, not two.
xyz I organized the nodes in this way, but the codes I use only support RayCast2D and when I make the main node Node2D, I get an error from the existing codes. Do you have any information about this?
- Edited
codernesim It's code, not codes. If you just copypasted the code from somewhere without understanding what it's doing then it's unlikely you'll get very far with this. Maybe start with a simpler project.
- Edited
xyz
No, I wrote this code and I know every detail.
Each object randomly assigned to the scene actually has an ID number and a color.
The image below is an example of this.
Then this Line2D will get the color of each targeted object
“if get_collider().color != null:
vl.default_color = get_collider().color
dl.default_color = get_collider().color”
I used this line of code.
Then I wrote code to terminate the length of this after reaching the object, and what I explained and what I wanted actually works correctly for FirstLine, but the same features are not applied for SecondLine.
My only problem now is that the SecondLine does not detect the color of that object and the Line2D length does not end after reaching the object.
This is the problem in general.
My Code:
extends RayCast2D
@onready var vl = $FirstLine
@onready var dl = $SecondLine
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 $"../../Control3".was_shooted or not $"../../Control4".was_shooted or not $"../../Control5".was_shooted:
vl.visible = true
dl.visible = true
if $"../../Control3".can_be_shot or $"../../Control4".can_be_shot or $"../../Control5".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.visible = false
else:
vl.visible = false
dl.visible = false
codernesim The code that sets up raycasts is what's important here. I don't see any of that in the snippets you posted. Also post a screenshot with actual rays visible. Enable them in the editor Debug menu as I mentioned earlier.
xyz
I did not write any code for Raycast, I only wrote code for Line2D. Can you give me an idea if I need to write code for Raycast.
Also how can I see it in the debug section?
FirstLine:
SecondLine:
codernesim I did not write any code for Raycast
Who wrote the raycast code?
codernesim Also how can I see it in the debug section?
Debug -> Visible collision shapes.
xyz
I have only this code about Raycast.
The screenshots you mentioned are as follows.
- Edited
codernesim Well there you have it. There is no cast ray (red line) aligned with your second line. You need to position and rotate the raycast node so that its ray coincides with the line.
xyz
That's how it works on the stage.
Is there anything wrong here?
Also, if I need to position it, where can I do it, with code or on the stage?
codernesim You need to do it via code because the position/orientation of the second raycast depends on the rayhit result of the first raycast.
xyz
I understood what you wanted to say and learned more about the cause of the error.
So how can I solve this in the first codes I gave?
Do you have any idea?
- Edited
codernesim Start by using only 2 ray cast nodes. If one collides with a wall, set other's origin to its collision point and its look vector to the look vector of the first raycast reflected around the hit normal. Best to operate in global space to avoid local coordinate system confusion.
- Edited
xyz
I set the Raycast2Ds as you said and the second Raycast is now active and the second Raycast is now visible and working the way I want it to.
But the problem here is that now the SecondLine color added to the second Raycast is not the same as the first one.
I think the second Raycast does not have the properties of the first Raycast.
How can I fix this, my code is below.
extends RayCast2D
@onready var vl = $FirstRay
@onready var dl = $SecondRay
@onready var one = $FirstRay/FirstLine
@onready var two = $SecondRay/SecondLine
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 $"../Control3".was_shooted or not $"../Control4".was_shooted or not $"../Control5".was_shooted:
vl.visible = true
dl.visible = true
if $"../Control3".can_be_shot or $"../Control4".can_be_shot or $"../Control5".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())
one.default_color = default_color
two.default_color = default_color
one.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:
one.default_color = get_collider().color
two.default_color = get_collider().color
else:
one.default_color = default_color
two.default_color = default_color
one.points[1].x = collision_point.x
dl.visible = false
else:
one.default_color = default_color
one.points[1].x = vector_line_length
dl.visible = false
else:
vl.visible = false
dl.visible = false
codernesim Your code never checks if second raycast is colliding.
xyz
Is it possible to provide this collision with code?
If possible, how should I do it?
Thanks for your help.
- Edited
codernesim Try calling is_colliding()
on the second raycast node.
xyz
I did as you said and it was fixed in a few transactions.
Now it works the way I want it to.
Thank you for your help.