So I want the Sprite2D to smoothly go up and down but when I tested it, it didn’t work. Any help?
extends Sprite2D
@onready var audio_stream_player_2d: AudioStreamPlayer2D = $"../AudioStreamPlayer2D"
var original_y: float
var amp :float = 50
var freq : float = 2
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
original_y = position.y
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
var time = Time.get_ticks_msec() /1000
position.y = original_y+sin(time * freq *TAU)* amp
if Input.is_action_just_pressed("End"):
audio_stream_player_2d.stop()
get_tree().quit()
I see that you are grabbing the original_y on the _ready,
are you trying to make something of like a bobbing effect, when you mean by smoothly go up and down? you can probably do that with a tween, something like this:
you can probably modify the bob_duration to change how fast it bobs depending on your preference. Also to disable the bobbing, you can just kill the tween.
This should give you an integer division warning. try dividing by 1000.0, without decimal places it may be an integer which only produces whole numbers, so one second at time in this given equation.
Make sure to explain how things break, it’s very difficult to help if you do not explain both what you expected to happen and what really happens, how the two conflict.