Godot Version
4.4
Question
If I add a Line2D in my level scene then a “laser” between my player and target looks exactly how I would expect
However, if I build the laser inside my player class it’s always skewed .
I should note that inside the player scene I have a RayCast2D node which tells me if I’m pointed at a target. That returns a collider. I then get the position of the collider and try to build my Line2D between the player and the target.
$Laser.clear_points()
$Laser.add_point(position)
$Laser.add_point(collider.position)
NOTE: Using global_position rather than position doesn’t seem to make any difference.
The Line2D is always either offset or rotated in ways that don’t make sense.
Here’s the debug position and rotation logs:
Laser Rotation : 0.0
Player Rotation : -179.775314331055
Player position : (455.9517, 247.9171)
Player global_position : (455.9517, 247.9171)
Target position : (300.0, 250.0)
Target global_position : (300.0, 250.0)
Interestingly enough, making the first point in the Line2D be a Vector2.ZERO at least sets the starting position of the Line2D correctly.
$Laser.clear_points()
$Laser.add_point(Vector2.ZERO)
$Laser.add_point(collider.position)
But now, if I come at the target from a different direction, the Line2D is still rotated and is far too long.
Here is the position and rotation data for this screen shot:
Laser Rotation : 0.0
Player Rotation : -89.7761840820313
Player position : (305.7653, 376.46)
Player global_position : (305.7653, 376.46)
Target position : (300.0, 250.0)
Target global_position : (300.0, 250.0)
Because the Vector2.Zero is working I’m thinking that the position of the other end of the line must somehow be a relative position to the player object. I’ve tried subtracting the player position from the collider position and several other things but I just can’t seem to get a consistent Line2d between the player and the target with a Line2D as a child of the Player Scene.