Water trail (Line2D) for boat appears offset and not in the center of the boat or near it.

Hi everyone!

I’m trying to create a water trail effect behind my boat in Godot 4.4.1.stable using a Line2D node. The trail should follow the boat, but I’m running into an issue where the trail is always offset from the boat’s position (it’s not directly behind it). It doesn’t matter is Line2D is in SubViewport or out of it the result is still the same, it’s offset.

Here is my code of it:

extends Line2D

@export var MAX_LENGTH: int = 10
@export var sub_viewport: SubViewport
@export var parent_node: Node2D

@export var distance_at_largest_width: float = 16 * 6
@export var smallest_tip_width: float = 1.0
@export var largest_tip_width: float = 10.0

var length: float = 0.0
var queue: Array = []
var offset: Vector2i

func _ready() -> void:
	offset = Vector2i(sub_viewport.size.x / 2, sub_viewport.size.y / 2)

func _process(delta: float) -> void:
	length = 0.0

	var pos: Vector2 = parent_node.global_position + Vector2(offset)
	queue.append(pos)
	if queue.size() > MAX_LENGTH and queue.size() > 2:
		queue.pop_front()

	clear_points()
	var queue_array = queue.duplicate()
	for i in range(queue_array.size() - 1):
		length += queue_array[i].distance_to(queue_array[i + 1])
		add_point(parent_node.to_local(queue_array[i]))
	add_point(parent_node.to_local(queue_array[queue_array.size() - 1]))

	var t = clamp(length / distance_at_largest_width, 0, 1)
	var width_value = lerp(smallest_tip_width, largest_tip_width, t)
	if width_curve.get_point_count() > 0:
		width_curve.set_point_value(0, width_value)

func reset_line() -> void:
	clear_points()
	queue.clear()

No matter how I move the boat, the trail is always offset to one side and not following the boat directly. Even when I disable the viewport, the offset remains. I want the trail to appear right behind the boat (ideally starting from the back or the center of the boat sprite).

Here is how it looks in the game:

Here is my boat.tscn:

Why is the SubViewport in the tree?

What’s assigned to parent_node?

If you can, I’d suggest moving your Line2D node up to be a direct child of WaterTrail, make sure WaterTrail has a position that’s at or near (0, 0), and draw your lines as if the boat was at the origin.

Thanks a lot, that helped!

The SubViewport was there to make the trail pixelized, because It’s too distinguishing that the trail is regular, not pixelized, and the game itself is pixelart styled

1 Like

Fair enough. Is it working the way you want now?

Yes, thank you once more