I'm trying to figure out why my Line2D is rotating

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.

Are you sure the line is a child of FirstLevel? was that only for an example?

If not can you show the scene tree for the line? It seems like it’s a child of something offset by the barrel, you may need to use to_local with the Line2D

$Laser.clear_points()
$Laser.add_point(Vector2.ZERO)
$Laser.add_point($Laser.to_local(collider.position))
1 Like

If I understand you correctly the problem is when the laser is a child of player? In this case the transform of the player is also applied to the child. That means that the child’s position and rotation is relative to the player - this is the ‘local space’ of the line. This is why when setting the Line2Ds position to Vector2.Zero it is at the players position.
Ray-casts return results in ‘global space’ (e.g., get_collision_point).

This might be helpful : Matrices and transforms — Godot Engine (stable) documentation in English

That seems to still shift the line. But it moves even further away

You are correct. The Laser (Line2D) is a child of the player

I will review that document you linked to see if I can figure out what the issue is.

It turns out the solution was to_local.

I just implemented it incorrectly

$Laser.clear_points()
$Laser.add_point(Vector2.ZERO)
$Laser.add_point(to_local(collider.position))

This works.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.