Rotate an Entity - Continuous Spin
What you'll learn:
- Rotating entities with
rotateTo()insideonUpdate() - Tracking angles with script variables
- Choosing rotation axes with
simd_float3 - Building USC scripts entirely in Xcode
Time: ~7 minutes
Prerequisites:
- Untold Engine Studio open with any entity selected (a cube makes rotation easy to see)
- Access to Xcode via Scripts: Open in Xcode (toolbar button)
What We're Building
A rotation script that:
- Spins an entity around a chosen axis
- Uses a variable to track the current angle
- Lets you adjust spin speed without rewriting the script
Step 1: Create the Script
- Open Untold Engine Studio
- In the toolbar, click Scripts: Open in Xcode (blue button)
- Click the
+button and enter the script name:RotateAnEntity - Click OK, then Xcode opens the Scripts package
When the script is created:
- The source file is added to your project
- You edit the script in Xcode
You'll see a template like this:
import Foundation
import UntoldEngine
import simd
extension GenerateScripts {
static func generateRotateAnEntity(to dir: URL) {
// Write your script here
}
}
This script is added to GenerateScripts, which is the entry point the engine uses to discover and generate USC scripts.
Step 2: Wire Up the Script
⚠️ IMPORTANT MANUAL STEP
The editor created your script file, but you need to tell the build system to generate it.
- Click Scripts: Open in Xcode (blue button in toolbar)
- In Xcode, open GenerateScripts.swift
- Add your script to the main() function:
@main
struct GenerateScripts {
static func main() {
print("🔨 Generating USC scripts...")
let outputDir = URL(fileURLWithPath: "Generated/")
try? FileManager.default.createDirectory(at: outputDir, withIntermediateDirectories: true)
// Add this line:
generateRotateAnEntity(to: outputDir)
print("✅ All scripts generated in Generated/")
}
}
Now run the GenerateScripts target (Cmd+R) to generate the .uscript files.
MPORTANT NOTES: The function name (e.g. generateRotateAnEntity) MUST match the tutorial’s script Only adjust that single line per tutorial The structure, wording, and warning must remain consistent across all documents
Step 3: Write the Script
Replace the function with this complete script:
import Foundation
import UntoldEngine
import simd
extension GenerateScripts {
static func generateRotateAnEntity(to dir: URL) {
let script = buildScript(name: "RotateAnEntity") { s in
// Runs once when the entity starts
s.onStart()
.setVariable("rotationSpeed", to: 2.0) // degrees per frame
.setVariable("currentAngle", to: 0.0)
.setVariable("axis", to: simd_float3(x: 0, y: 1, z: 0))
.log("RotateAnEntity ready")
// Runs every frame
s.onUpdate()
.addFloat("currentAngle", "rotationSpeed", as: "nextAngle")
.setVariable("currentAngle", to: .variableRef("nextAngle"))
.rotateTo(
degrees: .variableRef("currentAngle"),
axis: simd_float3(x:0,y:1,z:0)
)
}
let outputPath = dir.appendingPathComponent("RotateAnEntity.uscript")
try? saveUSCScript(script, to: outputPath)
print(" ✅ RotateAnEntity.uscript")
}
}
Understanding the Code
rotationSpeed - Controls how much to rotate each frame
- Higher values spin faster
- Change this without touching the rest of the script
currentAngle - Tracks total rotation
- Prevents drift and keeps rotation smooth
axis - A simd_float3 defining which way to spin
(0, 1, 0)spins around Y (like a turntable)(1, 0, 0)spins forward/backward(0, 0, 1)spins left/right
rotateTo() - Sets an absolute rotation
- Uses the stored angle each frame
- Cleanly resets if you set
currentAngleback to0
Step 4: Build the Script
- In Xcode, press
Cmd+Rto run the GenerateScripts target and generate the.uscript(optional:Cmd+Bfirst to verify the build). - Watch for the Xcode run output:
🔨 Generating USC scripts...
✅ RotateAnEntity.uscript
✅ All scripts generated in Generated/
First build? May take 30-60 seconds to download engine dependencies. Subsequent builds are much faster.
Step 5: Attach to an Entity
- Return to Untold Engine Studio
- Select the entity you want to rotate
- In the Inspector panel, click Add Component → Script Component
- In the Asset Browser, find
RotateAnEntity.uscriptunder Scripts/Generated - Double click on the
.uscript. The script will be linked to the entity
Step 6: Test It!
- Click Play in the toolbar
- Watch the entity spin around the Y-axis
- Open the Console view to confirm:
RotateAnEntity ready - Click Stop to exit Play mode
Understanding the Output
- The entity rotates smoothly using
rotationSpeeddegrees per frame - Changing
axischanges the spin direction without new code - Resetting
currentAngleto0realigns the entity instantly
⚠️ Consistency Note: Large rotationSpeed values can cause visible jumps. Increase gradually for smoother motion.
Modify and Experiment
Try these changes to learn more:
Rotate with rotateBy()
s.onUpdate()
.rotateBy(
degrees: 1.0,
axis: simd_float3(x: 0, y: 1, z: 0)
)
Spin on Multiple Axes
.setVariable("axis", to: simd_float3(x: 1, y: 1, z: 0)) // diagonal spin
Start Facing a Specific Direction
s.onStart()
.rotateTo(
degrees: 45.0,
axis: simd_float3(x: 0, y: 1, z: 0)
)
.setVariable("currentAngle", to: 45.0)
After making changes:
- In Xcode, press
Cmd+Rto rerun the GenerateScripts target and regenerate the scripts (Cmd+Boptional first). - Click Reload in the Script Component Inspector
- Test in Play mode
What You Learned
✅ Creating a rotation script in Untold Engine Studio
✅ Tracking angles with script variables
✅ Rotating entities around any axis
✅ Building and attaching scripts to entities
✅ Testing rotations in Play mode