Carrying: pick up, carry, throw and hand over
Holdableon a prop: this can be held, and here is where it sits.Carrieron the player's body: the player's controls pick up, put down, throw and hand over.@builtin::modules.carry: the same capability for any script.
The two components
-- a prop: any entity with a dynamic Physics body
entity.spawn("crate", {
synced = true,
position = { 1, 0.3, -6 },
components = {
Model = { model = "cube" },
BoxCollider = {},
Physics = { kind = "dynamic" },
Holdable = { grip = "bodyFront", gripPosition = { 0, 0, 0.3 } },
},
})
-- the player's body (the `body` of a PlayerPrototype, in a scene's build.luau)
body.component.add("Carrier", {})
synced = true makes the prop one body every player shares. A world started
from the campfire clearing already has a Carrier on its player body and its
logs and pebbles marked Holdable.
Where it is held
Holdable.grip names the grip point: rightGrip (the default) and leftGrip
are the palms, bodyFront carries a big thing before the chest, faceFront
holds one at eye level. gripPosition (metres) and gripRotation (degrees)
place it in that point's frame, +Z forward and +Y up. On a body that is no
humanoid, grip names an entity under it (mouth, frontPaw).
ball.component.add("Holdable", {}) -- in the right hand
lantern.component.add("Holdable", { gripPosition = { 0, -0.2, 0 } }) -- hanging below the fist
crate.component.add("Holdable", { grip = "bodyFront", gripPosition = { 0, 0, 0.3 } })
The player's controls
One control does all of it, Interact on the default map (E, the gamepad's west button, the on-screen Interact button):
- with empty hands, a press picks up the nearest loose prop in front;
- holding something, a tap hands it to the player standing in front (the
nearest avatar with a
Carrierand empty hands withingiveReach, 2.5 m), and with nobody there puts it down; - a longer press winds up a throw that leaves on release, where the camera looks, harder the longer it was held.
"In front" is the way the body visibly faces. Carrier fields tune it:
reach, throwSpeed, minThrowSpeed, chargeSeconds, tapSeconds,
throwAngle, giveReach, acceptGifts, and throwControl to throw from a
second control. Carrier:receiver() names the avatar a tap hands to right now,
for a prompt such as "Tap E: give to Ana"; Carrier:held() names what the
player holds.
From code
local carry = require("@builtin::modules.carry")
local me = layers.active.players.localPlayer.avatar
carry.pickUp(me, carry.nearest(me)) -- (ok, why)
carry.throw(me, { 0, 3, -7 }) -- added to the hand's own velocity
carry.place(me, { 1, 0.2, 3 }) -- put down there, at rest
carry.give(me, friend.avatar) -- (ok, why): into the friend's hand
carry.heldBy(me), carry.holderOf(crate)
carry.nearestReceiver(me) is the avatar a tap would hand to, and
carry.facing(me) the way the body faces. The module's README lists every
call.
Across players
The peer that holds a prop owns it. Picking up a prop another player's peer
owns asks that peer for it first, and the call waits for the answer, so call it
from code that can yield. A prop handed to another player's avatar is offered
to that player's peer, which takes it into the avatar's hand when its
Carrier accepts gifts. While held, the prop is drawn against the holder's
body on every screen, and a thrown prop is a shared physics body again where it
lands. A prop with FluidForces floats in water or sinks by its density.