+1 vote
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?

in Engine by (13 points)

I was looking for a way to have a 3D node oscillate along the y axis, and your method of summing delta and using it in a trig function was perfect.

The only change I made was doing this in the physics_process() function.

Thanks

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.

by (680 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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.