Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

I'm making a 3D platformer in Godot and I have a StaticBody3D Node with mesh and a…

  1. 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.

  2. Assistant

    To make your StaticBody3D node 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:

    1. Create Variables: You'll need variables to keep track of the elapsed time and the starting position of the node.

    2. Use the Sine Function: The sine function is perfect for creating smooth, oscillating motion like moving up and down.

    3. 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 StaticBody3D node:

    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_position
    

    Explanation:

    • Variables:

      • time_passed keeps track of the total time elapsed since the game started.
      • start_position stores the original position of the node so that motion is relative to its starting point.
    • In _ready():

      • We initialize start_position with the node's global position when the scene is ready.
    • In _process(delta):

      • We increment time_passed by delta, which is the time elapsed since the last frame.
      • We calculate new_y using the sine function to create a smooth oscillation.
        • amplitude determines how high and low the node moves from its starting Y position.
        • speed controls how fast the node moves up and down.
      • We create a new position Vector3 with the updated Y value.
      • We update the node's global_transform.origin to the new position.

    Adjusting Movement:

    • Amplitude: Increase or decrease the amplitude variable to make the node move higher or lower.
    • Speed: Adjust the speed variable 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 a KinematicBody3D or AnimatedStaticBody3D for 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 StaticBody3D node 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 amplitude and speed values to suit the motion you desire. Happy game developing!