Moving platforms

In this mini-guide we will make platforms move smoothly from side to side in a sinusoidal motion.

We’re building on the work in Godot Quick Start - 3D Platformer, but the instructions and script should work for similar types of 3D scenes in Godot 4.

Attach Script

Name moving_platform.gd

extends Node3D

var time := 0.0
@export var time_offset := 0.0
@export var distance := 2.0
@export var distance_offset := 0.0
@export var speed := 2.0

@onready var startpos := position

func _process(delta):
	# Sine movement
	position = startpos
	position.x += cos((time + time_offset) * speed/distance) * distance + distance_offset

	time += delta

Moving Platform Properties

Now, there is just one problem. The player doesn’t move with the platform!! 😢

This is fortunately quite easy to fix: Just change the body type from StaticBody3D to CharacterBody3D:

Open In Editor

Change Type

CharacterBody3D

To make additional platforms moving, you just need to attach the same script to the platform nodes:

Drag moving_platform.gd

💡 In the example video above, I’ve set a different value for the Time Offset property on each platform so they move out of sync.

Now, you can extend the script to support other types of movements, or make separate scripts for each type of movement. I leave you with a few ideas:

Have fun!