Created
March 30, 2024 23:28
-
-
Save Shilo/4489e033e65b03156d866287d798e8c6 to your computer and use it in GitHub Desktop.
Godot 4 modular sway component to bounce the y position of a parent, up and down.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name SwayComponent extends Node | |
@export var sway_distance: float = 32 | |
@export var sway_duration: float = 2 | |
@onready var start_y: float = y | |
var y: float: | |
set(value): | |
var parent := get_parent() | |
if !parent: | |
return | |
parent.set_indexed("position:y", value) | |
get: | |
var parent := get_parent() | |
if !parent: | |
return 0 | |
var value = parent.get_indexed("position:y") | |
if value == null: | |
value = 0 | |
return value | |
func _process(_delta: float) -> void: | |
y = start_y + sin(TAU * Time.get_ticks_msec() / 1000.0 / sway_duration) * sway_distance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment