Making a Spinning Platform with Traits
Create a reusable Spin trait with schema and metadata, attach it via the wrench tool, and spin entities by reading spinSpeed. Learn Traits, schemas, metadata, and angular velocity.
Script
Hey Mangos, welcome back to Jamango! Academy! Today we’re diving into Traits — Jamango’s plug-and-play behaviours to achieve almost anything in your game. By the end, you’ll have a reusable "Spin" trait to make spinning platforms for your obby. You can attach this trait to almost anything and tweak its speed.
What are Traits?#
Traits are lines of code that we attach to objects in the world to control how those objects function. They’re a core part of Jamango games. Open the code menu (J) and bring up server.ts.
We’ll define a new trait named Spin. This trait will have:
- A unique ID ("Spin")
- A schema with a
spinSpeedvector (X, Y, Z) - Some metadata (name, description, icon, color)
Define the Spin trait#
tsexport const Spin = J.defineTrait( "Spin", J.schema.object({ spinSpeed: J.schema.vec3(), }), { name: "Spin", description: "Makes the object spin!", icon: "🔄", color: "#51f",
Recap:
- We create a variable
Spinand callJ.defineTraitto make it a trait. - The schema defines the shape of data: here a
vec3calledspinSpeed. - Metadata customizes how the trait looks in-game.
Attach the trait to a prop using the wrench tool:
- Select the prop, click "Add Trait", choose "Spin".
- Set
spinSpeed(e.g. Y-axis for upright rotation). - Ensure Motion Type is "Kinematic" if you don’t want gravity moving the platform.
Spin objects with code#
Now let’s find all entities that have the Spin trait and apply their angular velocity on game start.
tsJ.onGameStart(() => { const spinningObjects = J.getAllWithTraits([Spin]); for (const [entityId, spinningTrait] of spinningObjects) { J.setEntityAngularVelocity(entityId, spinningTrait.spinSpeed); } });
Explanation:
J.onGameStartruns once when the world loads on the server.J.getAllWithTraits([Spin])returns every entity with theSpintrait plus its data.- For each entity, we read
spinningTrait.spinSpeedand pass it toJ.setEntityAngularVelocity.
Wrap-up#
With a few lines of code, you’ve built a customizable spinning mechanic. Change the speed or direction per entity using the trait’s spinSpeed. Try different axes and values for unique obstacles.
Share your wild spinning setups in #Creator-Logs on Discord, ask questions any time, and get ready — next we’ll use Traits to make a Bounce Pad!