Skip to content

Spinning bobber

Compiles

The game ships a script that spins an entity: entity_mover_rotate. This one keeps everything it does and adds a bob, rising and falling while it turns, like a pickup waiting to be collected. The spinning is the parent’s; the bob is twenty lines.

spinning_bobber.vscript
// spinning_bobber.vscript: the shipped rotator, plus a gentle bob up and
// down.
//
// Everything about spinning comes from the parent: its fields (`period`,
// `axis`) still show up in the editor, and its code still runs. This script
// adds two fields and changes one function.
super "/data/catch/scripts/utility/entity_mover/entity_mover_rotate.vscript"
// How far it rises and falls from where it was placed, in world units.
let bob_height: float = 40.0;
// Seconds for one full rise and fall.
let bob_seconds: float = 2.0;
// Where it started, so the bob is around its placed position.
let mut start_position: vec3 = vec3(0, 0, 0);
// The parent's `created` finds the transform and starts the spin; keep
// that, then remember where we are.
event export fn created() {
super::created();
start_position = transform.position;
}
// The parent calls this every tick to turn the entity. Turn it as before,
// then move it up or down.
override fn update_rotation() {
super::update_rotation();
let seconds: float = $time.ms_to_seconds($time.get_gameplay_time_ms());
let wave: float = $math.sin(seconds / bob_seconds * 6.2831855);
transform.position = start_position + $math.vec3(0.0, 0.0, wave * bob_height);
}
  1. Name the parentlines 1-7

    super makes this script everything the shipped rotator is: its settings (period, axis), its state, its events.

  2. Add settings and statelines 9-16

    Two new settings appear in the editor alongside the parent’s, and one field remembers the starting position.

  3. Add to createdlines 18-23

    The parent’s created finds the transform and starts the spin. Call it first, then remember where the entity began. transform is the parent’s field, usable as if it were ours.

  4. Override the per-tick steplines 25-32

    The parent calls update_rotation every tick and marks it virtual, so it can be replaced. Run the parent’s version to turn, then set the height from the clock.

Put it on any entity with a model, in place of entity_mover_rotate. period (from the parent) sets seconds per turn; bob_height and bob_seconds set the bob.