### UdonSharp Behaviour Template for Smart Object Sync
Source: https://github.com/mmmaellon/smartobjectsync/blob/main/Assets/UdonSharp/UtilityScripts/ExampleUtilityTemplate.txt
A template UdonSharp behaviour script for VRChat, intended as a starting point for implementing smart object synchronization. It includes necessary VRC SDK namespaces for interaction.
```csharp
using UnityEngine;
using VRC.SDK3.Components;
using VRC.SDKBase;
using VRC.Udon;
namespace UdonSharp.Examples.Utilities
{
///
///
///
public class : UdonSharpBehaviour
{
}
}
```
--------------------------------
### Implement Custom Synchronized States with SmartObjectSyncState
Source: https://context7.com/mmmaellon/smartobjectsync/llms.txt
Illustrates how to create custom synchronized states by extending the SmartObjectSyncState class. This example, 'ZeroGravityState', demonstrates disabling gravity when entering the state and re-enabling it upon exit. It also shows how to handle serialization, interpolation, and state transitions. Requires UdonSharp, UnityEngine, VRC.SDKBase, and MMMaellon.
```csharp
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using MMMaellon;
public class ZeroGravityState : SmartObjectSyncState
{
public override void OnEnterState()
{
// Called when entering this state
sync.rigid.useGravity = false;
}
public override void OnExitState()
{
// Called when leaving this state
sync.rigid.useGravity = true;
}
public override void OnSmartObjectSerialize()
{
// Owner: Set synced variables before network send
sync.pos = transform.position;
sync.rot = transform.rotation;
sync.vel = sync.rigid.velocity;
sync.spin = sync.rigid.angularVelocity;
}
public override void OnInterpolationStart()
{
// Non-owners: Prepare for interpolation
if (sync.IsLocalOwner()) return;
sync.startPos = transform.position;
sync.startRot = transform.rotation;
sync.startVel = sync.rigid.velocity;
sync.startSpin = sync.rigid.angularVelocity;
}
public override void Interpolate(float interpolation)
{
// All players: Called each frame during interpolation (0.0 to 1.0)
if (sync.IsLocalOwner()) return;
transform.position = sync.HermiteInterpolatePosition(
sync.startPos, sync.startVel,
sync.pos, sync.vel,
interpolation
);
transform.rotation = sync.HermiteInterpolateRotation(
sync.startRot, sync.startSpin,
sync.rot, sync.spin,
interpolation
);
}
public override bool OnInterpolationEnd()
{
// Return true to keep update loop running, false to disable for optimization
if (sync.IsLocalOwner())
{
if (transform.position.y < sync.respawnHeight)
{
sync.Respawn();
}
return sync.rigid.velocity.y < 0; // Keep running while falling
}
return false; // Stop loop for non-owners
}
// Enter this custom state
public void ActivateZeroGravity()
{
EnterState(); // Sets sync.state = STATE_CUSTOM + stateID
}
// Exit to normal physics
public void DeactivateZeroGravity()
{
ExitState(); // Sets sync.state = STATE_FALLING
}
}
```
--------------------------------
### Attach Object to Avatar Bone with SmartObjectSync
Source: https://context7.com/mmmaellon/smartobjectsync/llms.txt
Provides examples of attaching a synchronized object to specific avatar bones using SmartObjectSync. This functionality allows objects to follow a player's avatar. It requires the MMMaellon namespace and UdonSharp.
```csharp
using UdonSharp;
using UnityEngine;
using MMMaellon;
public class BoneAttachmentExample : UdonSharpBehaviour
{
public SmartObjectSync sync;
public void AttachToHead()
{
sync.AttachToBone(HumanBodyBones.Head);
}
public void AttachToChest()
{
sync.AttachToBone(HumanBodyBones.Chest);
}
public void AttachToSpine()
{
sync.AttachToBone(HumanBodyBones.Spine);
}
public void AttachToLeftFoot()
{
sync.AttachToBone(HumanBodyBones.LeftFoot);
}
public void DetachFromBone()
{
// Exit to falling/interpolating state
sync.state = SmartObjectSync.STATE_INTERPOLATING;
}
}
```
--------------------------------
### Teleport Object with SmartObjectSync
Source: https://context7.com/mmmaellon/smartobjectsync/llms.txt
Demonstrates how to use the `TeleportToWorldSpace` and `TeleportToLocalSpace` methods of SmartObjectSync to move objects. It also shows how to reset an object to its spawn point using `Respawn`.
```csharp
using UdonSharp;
using UnityEngine;
using MMMaellon;
public class TeleportExample : UdonSharpBehaviour
{
public SmartObjectSync sync;
public Transform targetPosition;
public void TeleportToTarget()
{
// Teleport to world space position
sync.TeleportToWorldSpace(
targetPosition.position, // New position
targetPosition.rotation, // New rotation
Vector3.zero, // Initial velocity
Vector3.zero // Initial angular velocity
);
}
public void TeleportToLocalSpace()
{
// Teleport relative to parent transform
sync.TeleportToLocalSpace(
Vector3.up * 2f, // Local position offset
Quaternion.identity, // Local rotation
Vector3.forward * 5f, // Initial velocity
Vector3.zero // Initial angular velocity
);
}
public void ResetToSpawn()
{
// Respawn to original position/state
sync.Respawn();
}
}
```
--------------------------------
### Configure SmartObjectSync Component
Source: https://context7.com/mmmaellon/smartobjectsync/llms.txt
Sets up the core ownership and synchronization properties for the SmartObjectSync component in Unity. This script configures collision behavior, ownership transfer rules, and respawn settings.
```csharp
using UdonSharp;
using UnityEngine;
using MMMaellon;
// SmartObjectSync is added via Unity Inspector
// Configure these public properties:
public class SmartObjectSyncSetup : UdonSharpBehaviour
{
public SmartObjectSync sync;
void Start()
{
// Core ownership settings
sync.takeOwnershipOfOtherObjectsOnCollision = true; // Take ownership when hitting other objects
sync.allowOthersToTakeOwnershipOnCollision = true; // Let others take ownership on collision
sync.allowTheftFromSelf = true; // Allow stealing from your own hand
sync.syncParticleCollisions = false; // Sync particle collision events
sync.respawnHeight = -100f; // Y position that triggers respawn
}
}
```
--------------------------------
### Manage Object Ownership with SmartObjectSync
Source: https://context7.com/mmmaellon/smartobjectsync/llms.txt
Demonstrates how to control object ownership for network synchronization using SmartObjectSync. It covers taking ownership with and without congestion checks, checking current ownership, and verifying if the object is held or attached to a player. This requires the MMMaellon namespace and UdonSharp.
```csharp
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using MMMaellon;
public class OwnershipExample : UdonSharpBehaviour
{
public SmartObjectSync sync;
public void TakeControl()
{
// Take ownership without network congestion check
sync.TakeOwnership(false);
// Take ownership only if network isn't congested
sync.TakeOwnership(true);
}
public void CheckOwnership()
{
// Check if local player owns this object
if (sync.IsLocalOwner())
{
Debug.Log("You own this object");
}
// Get the current owner
VRCPlayerApi owner = sync.owner;
if (Utilities.IsValid(owner))
{
Debug.Log($"Owned by: {owner.displayName}");
}
}
public void CheckState()
{
// Check if object is being held
if (sync.IsHeld())
{
Debug.Log("Object is held in a hand");
}
// Check if attached to any player (held, bone, playspace)
if (sync.IsAttachedToPlayer())
{
Debug.Log("Object is attached to a player");
}
// Get current state as string
Debug.Log(sync.StateToString(sync.state));
}
}
```
--------------------------------
### Inventory State Management with InventoryState
Source: https://context7.com/mmmaellon/smartobjectsync/llms.txt
Manages object states for VR hand-accessible inventory systems. Configure item properties like scale and offsets, and enter different inventory states (e.g., left or right hand). Requires InventoryManager in the scene and InventoryState component on pickupable objects.
```csharp
using UdonSharp;
using UnityEngine;
using MMMaellon;
// Requires InventoryManager in scene
// Add InventoryState component to pickupable objects
public class InventoryExample : UdonSharpBehaviour
{
public InventoryState inventoryState;
public InventoryManager manager;
void Start()
{
// Configure inventory item properties
inventoryState.inventoryItemScale = 0.1f; // Scale when in inventory
inventoryState.inventoryPosOffset = Vector3.zero;
inventoryState.inventoryRotOffset = Vector3.zero;
}
public void StoreInLeftHand()
{
inventoryState.useLeftHandInventory = true;
inventoryState.EnterState();
}
public void StoreInRightHand()
{
inventoryState.useLeftHandInventory = false;
inventoryState.EnterState();
}
}
```
--------------------------------
### Pickup Control for Objects with SmartObjectSync
Source: https://context7.com/mmmaellon/smartobjectsync/llms.txt
Controls whether an object can be picked up by players. Provides methods to disable, enable, or toggle the pickupable state of an object. Can be managed directly via the 'pickupable' boolean or through dedicated methods.
```csharp
using UdonSharp;
using UnityEngine;
using MMMaellon;
public class PickupControlExample : UdonSharpBehaviour
{
public SmartObjectSync sync;
public void DisablePickup()
{
sync.pickupable = false;
}
public void EnablePickup()
{
sync.pickupable = true;
}
public void TogglePickup()
{
sync.TogglePickupable();
}
// Alternative methods
public void UseDirectMethods()
{
sync.DisablePickupable(); // Same as pickupable = false
sync.EnablePickupable(); // Same as pickupable = true
}
}
```
--------------------------------
### Network Serialization Control with SmartObjectSync
Source: https://context7.com/mmmaellon/smartobjectsync/llms.txt
Manually triggers network synchronization for objects. Includes functionality to check for network congestion before sending updates and retrieve network timing information like latency and interpolation progress.
```csharp
using UdonSharp;
using UnityEngine;
using MMMaellon;
public class NetworkExample : UdonSharpBehaviour
{
public SmartObjectSync sync;
public void ForceSync()
{
// Request network serialization (respects congestion)
sync.Serialize();
}
public void CheckNetwork()
{
// Check if network is congested
if (sync.IsNetworkAtRisk())
{
Debug.Log("Network congested, delaying sync");
}
else
{
sync.Serialize();
}
}
public void GetNetworkTiming()
{
// Get estimated network latency
float lag = sync.lagTime;
Debug.Log($"Estimated lag: {lag}s");
// Get current interpolation progress (0.0 to 1.0+)
float progress = sync.interpolation;
Debug.Log($"Interpolation: {progress}");
}
}
```
--------------------------------
### Hermite Interpolation for Smooth Movement with SmartObjectSync
Source: https://context7.com/mmmaellon/smartobjectsync/llms.txt
Utilizes built-in Hermite interpolation functions for smooth position and rotation updates. This is useful for animating objects or ensuring smooth transitions during network synchronization, even with varying latency.
```csharp
using UdonSharp;
using UnityEngine;
using MMMaellon;
public class InterpolationExample : UdonSharpBehaviour
{
public SmartObjectSync sync;
public void DemoInterpolation()
{
Vector3 startPos = Vector3.zero;
Vector3 startVel = Vector3.forward * 5f;
Vector3 endPos = Vector3.forward * 10f;
Vector3 endVel = Vector3.zero;
float t = 0.5f; // 0.0 to 1.0
// Smooth position interpolation considering velocities
Vector3 interpolatedPos = sync.HermiteInterpolatePosition(
startPos, startVel,
endPos, endVel,
t
);
Quaternion startRot = Quaternion.identity;
Vector3 startSpin = Vector3.zero;
Quaternion endRot = Quaternion.Euler(0, 90, 0);
Vector3 endSpin = Vector3.zero;
// Smooth rotation interpolation
Quaternion interpolatedRot = sync.HermiteInterpolateRotation(
startRot, startSpin,
endRot, endSpin,
t
);
}
}
```
--------------------------------
### SmartObjectSync State Constants
Source: https://context7.com/mmmaellon/smartobjectsync/llms.txt
Defines the integer constants representing the built-in states for SmartObjectSync. These states dictate how an object behaves in the synchronized environment, from resting to being held or custom states.
```csharp
// Built-in state constants
public const int STATE_SLEEPING = 0; // Object at rest, rigidbody sleeping
public const int STATE_TELEPORTING = 1; // Instant position sync
public const int STATE_INTERPOLATING = 2; // Smooth Hermite interpolation
public const int STATE_FALLING = 3; // Projectile motion with bounce simulation
public const int STATE_LEFT_HAND_HELD = 4; // Held in left hand
public const int STATE_RIGHT_HAND_HELD = 5; // Held in right hand
public const int STATE_NO_HAND_HELD = 6; // Held but no hand bones (desktop)
public const int STATE_ATTACHED_TO_PLAYSPACE = 7; // Attached to player position
public const int STATE_WORLD_LOCK = 8; // Locked in world space
public const int STATE_CUSTOM = 9; // Custom state (offset by stateID)
// Bone attachment states use negative values: -1 - (int)HumanBodyBones.X
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.