0 votes
extends Node2D

onready var target = $"../target".position
var total_time = 0
var amplitude = 10

func _draw():
    draw_line(Vector2(), target - position, Color.aqua)

func _process(delta):
    target.y = cos(total_time) * amplitude
    total_time += delta
    update()

Suppose I have two 2D nodes ("Node2D" and "target") with a script above attached to "Node2D". They are both children of 2D node called "Main".

What I'm having trouble with is that target's y is oscillating relative to its parent (having sinusoidal peaks relative to its parent), and I want these sinusoidal peaks to be relative to target's original position that was defined in the scene if that makes sense.

How can I achieve that?

asked Jul 6 in Engine by Degermark (12 points)

1 Answer

0 votes

In the _draw() call, you're using the Node2D's position, not the target's position.
Just to verify, I tried to replicate your scene tree:
tree
This tree, as you've described it, is a little unusual. I would try to avoid using a script in one node to regularly determine the actions of another node unless it is explicitly required or helpful. As it stands now, this entire scene could be 1 node: target with an attached script.

answered Jul 6 by DDoop (395 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.