I'm making a 3D platformer in Godot and I have a StaticBody3D Node with mesh and a…
- User
I'm making a 3D platformer in Godot and I have a StaticBody3D Node with mesh and a collision shape set up as child nodes. I would like to change the script attached to the StaticBody3D node so that the node moves up and down repeatedly when I start the game. Can you propose a solution? I assume some Vector3 shenenigans are needed.
- Assistant
To make your
StaticBody3Dnode move up and down repeatedly in Godot, you can adjust its position over time in the_process(delta)function. Here's how you can do it:-
Create Variables: You'll need variables to keep track of the elapsed time and the starting position of the node.
-
Use the Sine Function: The sine function is perfect for creating smooth, oscillating motion like moving up and down.
-
Update the Position: In the
_process(delta)function, update the node's position based on the sine of the elapsed time.
Here's a script you can attach to your
StaticBody3Dnode:extends StaticBody3D # Variable to keep track of elapsed time var time_passed: float = 0.0 # Store the starting position of the node onready var start_position: Vector3 = Vector3.ZERO func _ready(): # Initialize the starting position when the node is ready start_position = global_transform.origin func _process(delta): # Increment elapsed time time_passed += delta # Adjust these values to change the amplitude and speed var amplitude = 2.0 # The maximum height difference var speed = 1.5 # How fast the node moves up and down # Calculate the new Y position using sine wave var new_y = start_position.y + amplitude * sin(time_passed * speed) # Set the new position while keeping X and Z the same var new_position = Vector3(start_position.x, new_y, start_position.z) global_transform.origin = new_positionExplanation:
-
Variables:
time_passedkeeps track of the total time elapsed since the game started.start_positionstores the original position of the node so that motion is relative to its starting point.
-
In
_ready():- We initialize
start_positionwith the node's global position when the scene is ready.
- We initialize
-
In
_process(delta):- We increment
time_passedbydelta, which is the time elapsed since the last frame. - We calculate
new_yusing the sine function to create a smooth oscillation.amplitudedetermines how high and low the node moves from its starting Y position.speedcontrols how fast the node moves up and down.
- We create a new position
Vector3with the updated Y value. - We update the node's
global_transform.originto the new position.
- We increment
Adjusting Movement:
- Amplitude: Increase or decrease the
amplitudevariable to make the node move higher or lower. - Speed: Adjust the
speedvariable to make the movement faster or slower.
Note:
- Since you're moving a
StaticBody3D, ensure that any dynamic bodies interacting with it are correctly updated. In some cases, you might need to use aKinematicBody3DorAnimatedStaticBody3Dfor moving platforms to get the best physics interactions. - If you face any issues with collision detection, consider switching the node type or updating the collision shape accordingly.
Result:
With this script, when you start the game, the
StaticBody3Dnode will smoothly move up and down along the Y-axis, creating the effect of a moving platform or object in your 3D platformer.
Feel free to adjust the
amplitudeandspeedvalues to suit the motion you desire. Happy game developing! -