Below is a complete GDScript example for Godot 4.3 that implements a sine wave function and uses it to move a Node2D up and down. I've taken into account the screen resolution (1920x1080) by making the amplitude of the sine wave adjustable and optionally relative to the screen height. This ensures the movement scales well across different resolutions.
Key Points:
Sine Wave Function: I've created a reusable function called sine_wave(t: float) that takes a time parameter and returns the y-offset based on a sine wave. The amplitude and frequency are defined as variables in the script, so you can easily tweak them.
Node Movement: The Node2D's position is updated in the _process(delta) function, which runs every frame. I use delta to accumulate time, making the movement smooth and frame-rate independent.
Resolution Handling: The amplitude can be set to a fixed value (e.g., 100 pixels) or made relative to the screen height (e.g., 5% of the screen height). I've included a commented-out line in _ready() to demonstrate how to make it resolution-aware. This way, on a 1920x1080 screen, an amplitude of 5% would be about 54 pixels.
Customization: You can adjust the amplitude and frequency variables to control the height of the oscillation and how fast it cycles. For example:
amplitude: Controls how far the node moves up and down (in pixels).
frequency: Controls how many full cycles occur per second (e.g., 2.0 means 2 cycles per second).
How to Use: Attach this script to a Node2D in your Godot scene. The node will start oscillating vertically around its initial y-position once the game runs.
Here's the GDScript code:
gdscript
extends Node2D
# Variables for the sine wave
var time_elapsed: float = 0.0 # Tracks the accumulated time for smooth movement
var amplitude: float = 100.0 # Amplitude in pixels (how high/low the node moves). Adjust this value.
var frequency: float = 2.0 # Frequency in cycles per second (how fast the wave oscillates)
# Store the base y-position of the node
var base_y: float
func _ready():
# Set the base y-position to the node's initial position
base_y = position.y
# Optional: Make amplitude relative to screen resolution (e.g., 1920x1080)
# This ensures the movement scales with different screen sizes.
# Uncomment and adjust the multiplier (e.g., 0.05 for 5% of screen height)
# var screen_height = get_viewport_rect().size.y
# amplitude = screen_height * 0.05 # Example: 5% of screen height for resolution independence
func _process(delta: float):
# Accumulate time using delta for frame-rate independent movement
time_elapsed += delta
# Calculate the y-offset using the sine wave function
var y_offset = sine_wave(time_elapsed)
# Update the node's y-position, keeping x-position unchanged
position.y = base_y + y_offset
# Sine wave function: returns the y-offset based on time
# t: The time parameter (in seconds)
func sine_wave(t: float) -> float:
# Standard sine wave formula: A * sin(2 * PI * frequency * t)
# Using TAU (which is 2 * PI in Godot) for cleaner code
return amplitude * sin(TAU * frequency * t)
How It Works:
Sine Wave Function: The sine_wave(t) function computes a sine wave based on the accumulated time. It uses Godot's built-in TAU constant (equivalent to 2 * π) for accuracy.
Movement in _process: Every frame, the script adds the delta time to time_elapsed, calculates the y-offset, and applies it to the node's position. This creates a smooth up-and-down oscillation.
Resolution Consideration: By default, the amplitude is fixed at 100 pixels. If you uncomment the line in _ready(), the amplitude will be based on the current viewport size, making it adaptable to any resolution (e.g., on 1920x1080, 5% height = 54 pixels).
Testing and Adjustments:
In the Godot Editor: Attach this script to a Node2D (e.g., a Sprite2D or any other Node2D). Run the scene and observe the node moving up and down.
Tuning Parameters:
Increase frequency for faster oscillations (e.g., frequency = 5.0 for 5 cycles per second).
Change amplitude to control the range of movement (e.g., amplitude = 200.0 for larger swings).
If you want resolution independence, use the commented line in _ready() and test on different window sizes.
Performance: This uses _process, which runs every frame. If you need better performance, you could switch to _physics_process for physics-based updates, but _process is fine for simple visual effects.
If you need further modifications, such as adding horizontal movement, changing the wave type, or making the function more generic, let me know!