In godot 4.3, i need you to make a sin wave as a function, then move a node 2d up and…
- User
In godot 4.3, i need you to make a sin wave as a function, then move a node 2d up and down via this sinwave, keep in mind the resoltion, its 1920x1080
Assistant
Here's a complete solution for creating a sine wave movement in Godot 4.3:
1. Create the Sine Wave Function
First, create a new script for your Node2D (let's call it
SineWaveMover.gd):extends Node2D # Configuration variables @export var amplitude: float = 100.0 # How far up/down it moves (in pixels) @export var frequency: float = 1.0 # How fast it oscillates (cycles per second) @export var vertical_offset: float = 540.0 # Center position (1080/2 = middle of screen) # Internal variables var time: float = 0.0 var initial_y: float func _ready(): # Store the initial Y position if you want to use it as offset initial_y = position.y # Set initial position to center screen vertically position.y = vertical_offset func _process(delta): time += delta # Calculate new Y position using sine wave var new_y = vertical_offset + amplitude * sin(time * frequency * TAU) position.y = new_y2. Alternative: More Flexible Version
Here's a more flexible version that allows you to control more parameters:
extends Node2D @export_group("Sine Wave Movement") @export var amplitude: float = 100.0 @export var frequency: float = 1.0 @export var phase_shift: float = 0.0 # Horizontal shift of the wave @export var center_y: float = 540.0 # 1080/2 for center of 1920x1080 @export var use_initial_position: bool = false func _process(delta): # Calculate sine wave var sine_value = sin((Time.get_time_dict_from_system()["unix"] + phase_shift) * frequency * TAU) # Set position based on sine wave if use_initial_position: position.y = initial_y + amplitude * sine_value else: position.y = center_y + amplitude * sine_value func _ready(): if use_initial_position: initial_y = position.y3. Usage Instructions
- Create a Node2D in your scene
- Attach the script to your Node2D
- Adjust the exported variables in the inspector:
- Set
amplitudeto control how far it moves up/down (e.g., 200 for ±200 pixels) - Set
frequencyto control speed (e.g., 0.5 for slower, 2.0 for faster) - Set
center_yto 540 for vertical center of 1920x1080 resolution
- Set
4. Pure Function Version (if you need just the math)
If you need just the sine wave calculation as a reusable function:
# Returns a sine wave value between -1 and 1 func get_sine_wave(time: float, frequency: float = 1.0, phase_shift: float = 0.0) -> float: return sin((time + phase_shift) * frequency * TAU) # Returns a mapped sine wave value for positioning func get_sine_position(time: float, amplitude: float, center: float, frequency: float = 1.0) -> float: return center + amplitude * get_sine_wave(time, frequency)5. Example with Sprite
If you want to test it with a visible sprite:
- Add a Sprite2D as a child of your Node2D
- Set the Sprite2D's texture to any image
- The Node2D will move up and down while the sprite follows
The movement will be smooth and centered vertically on your 1920x1080 resolution screen.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.