Component:Snapper
More actions

The Snapper component allows a slot to parent to Snap Target slots when they are dropped close together.
It is popular for accessories such as hats and glasses as well as wherever an item needs to snap into or onto a spot and stay there, such as a replaceable rose in a vase, or a cup on to a saucer.
If you want to snap to a surface instead of a specific slot, see: GrabbableReceiverSurface
Fields
| Name | Type | Description |
|---|---|---|
persistent
|
Bool | Determines whether or not this item will be saved to the server. |
UpdateOrder
|
Int | Controls the order in which this component is updated. |
Enabled
|
Bool | Controls whether or not this component is enabled. Some components stop their functionality when this field is disabled, but some don't. |
UseBoundingBoxCenter
|
Bool | Causes the Snapper to use the center of its bounding box rather than its origin point. |
SnapCheckRadius
|
Float | Radius of the sphere overlap test around the snap point. Defaults to 0.01.
|
CheckStaticColliders
|
Bool | Allows the Snapper to look for colliders on the Snap Target's slot that are set to Static, in addition to colliders set to Trigger.
|
SnapTargetWhitelist
|
list of SnapTarget | List of Snap Targets that the slot can attach to. |
Keywords
|
direct SyncFieldList`1<String> | List of keywords to match against the corresponding keywords of a Snap Target. Multiple keywords allow snapping to multiple Snap Targets. |
Usage
Add the Snapper component to the root of the object you wish to snap. Make sure the slot also has a Grabbable component.
On the slot you wish to use as the target, add a SnapTarget component. This automatically adds a matching SphereCollider. Adjust MaximumSnapDistance to control the area the center of the snapper needs to be in to snap; this also changes the size of the matching collider. You can use the Visualize Collider button to see the area of effect.
Once the snapper is setup and confirmed working, set the appropriate SnapperKeywordWhitelist tags to limit unwanted snapping.
Snapped objects become children of the snap target, and the process also resets the scale, position and rotation of the snapper slot.
The actual reparenting only happens after the SnapTarget's AnimationTime has completed.
Scale problems
Because of the scale reset, there two situations that need to be considered.
- Creating new snappers
- It is best to keep the SnapTarget at a scale of 1 (Along with its rotation at 0), and to create a child slot under the Snapper slot containing the MeshRenderer that you can scale instead.
- This keeps everything more uniform and allows easier setup of other aspects.
- Using existing snappers
- Edit the SnapTarget scale/rotation to make the item match the avatar.
- Useful when it's not practical to edit the snapped item, for example if it's an existing set of glasses or hat from another avatar/user you wish to wear.
- Be aware that there are no standards for item scale, so creating a SnapTarget with appropriate
SnapperKeywordWhitelisttags for each item is a good idea.
Control over linking
There are a number of ways to control what a Snapper is allowed to link with on each SnapTarget.
SnapperKeywordWhitelist- A list of strings that need to match to allow the link.
- Most commonly used. It snaps any item that has a matching keyword on both sides.
SnapperWhitelist- A list of specific Snappers that are allowed to link.
- The list can be set up on either or both sides, and needs to reference the matching side.
- This gives the tightest control, but because it relies on references, it can break if items are copied or respawned.
Filters(SnapTarget only)- Allows filtering by function. For an example, see Component:MaterialTool#Sync Delegates.
Detecting snaps
Unlike grabbers, Snappers have no way to detect events directly (GH#2059).
You can use Dynamic variables that change when the snapped item is parented and becomes part of the parent's DynamicVariableSpace. Alternatively, use the VariableExists field of a DynamicVariableStatus component to detect a variable that exists in the parent space. This method is also useful for passing data across the snapper interface.
It can also be done with ProtoFlux. Note that these methods sometimes need additional checks to prevent false detection, for example of other parenting or grab systems.
- On the Snapper:
- Use ProtoFlux:GetParentSlot on the Snapper slot, then ProtoFlux:GetSlotName, then ProtoFlux:Equals to check whether the name matches the intended snap target.
- On the SnapTarget:
- Use ProtoFlux:ChildrenCount on the slot that contains the SnapTarget, and monitor whether it changes using ProtoFlux:Equals.
- For impulses, ProtoFlux:SlotChildrenEvents can be used to detect Snappers being attached or removed.
Getting data between Snappers and SnapTargets
Most of the time, Dynamic variables as mentioned above are the best way, but there is another method worth knowing about.
The Parent Values system allows a parent and child slot to exchange data without a full dynamic variable space. This allows more complicated interactions, overlapping Snappers and more precise control, as the link only works from parent to child and does not propagate beyond those two slots.
Behavior
When a Grabbable is dropped, the origin point of its slot (or the center of its slot's bounding box if UseBoundingBoxCenter is enabled) may be within a SnapTarget's SphereCollider. If so, the origin point of the dropped slot snaps to the origin point of the SnapTarget's slot.
If the SnapTarget's SphereCollider, or any collider on the Snapper or any of its children, is set to Active, the Snapper's slot snaps to the SnapTarget immediately upon collision with the Active collider, without needing to drop the Grabbable. This is the only way to make a Snapper use a collider instead of the origin point or bounding box center. Note: this also causes the Snapper's Enabled field to be ignored, so you need to enable or disable the Active collider instead.
Examples
Video
Covering the concept:
Covering hats (that can cover you):