I have a noobish question about moving a KinematicBody2D downwards via sine-wave - like a leaf dropping.

Here's the code I have:

func _physics_process(delta):
	
	time += delta
	
	var freq = 5
	var amplitude = 150
	
	var v = Vector2(0, 25)

	v.x = sin(time * freq) * amplitude

	velocity = v

	var collision = move_and_collide(velocity * delta)
	
	...

This almost works as intended. However, the "leaf drop" isn't centered relative to the node's x-position. It currently goes to the right side of the vertical axis, and bounces back to the "origin" (node's x-position). I'm having surprisingly difficult time to center this... I feel like I'm missing something obvious. I suspect I need to subtract something inside the sine-function, but my attempts have failed.

8 days later

Try using cosine instead of sine v.x = cos(time*freq)*amplitude Or if you wanna keep using sine then do this instead: v.x = sin(PI/2 + time*freq)*amplitude Either way I personally recommend using cosine

4 years later