Skip to main content

TossRandom

A class in Root.Township, the global namespace.

Makes a held object (such as a coin or die) spin randomly when tossed upward. The server detects a toss on release and picks a spin, then syncs it to clients in the chunk.

Constructors​

MemberWhat it is
TossRandom()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

14 private and internal fields
MemberWhat it is
private ParticleSystem androidParticlesParticle system for Android. Not used in this code.
private float horizontalMaximumMost horizontal speed a release can have and still count as a toss.
private Vector3 localBaseVelocitySerialized vector that is not used in this code.
private float oldMaxAngularVelocityThe rigidbody's original angular velocity cap, restored after a spin.
private ParticleSystem particlesParticle system played when the spin starts.
private float peakMaximumUpward speed below which the toss is near its peak and the spin is applied.
private Pickup pickupThe pickup on this object, used to hear grabs and releases.
private float randomVelocityMultiplier on the random unit spin direction, giving the angular speed.
private float spinDelaySeconds between the spin starting (particles) and the angular velocity being applied.
private float spinUpBoostExtra upward speed added when the spin is applied.
private MethodSyncStruct<Vector3> syncSpinSync message (SyncA) that sends the chosen spin direction to clients.
private Rigidbody targetRigidbodyThe rigidbody that is spun.
private float tossMinimumUpward speed a release needs to count as a toss.
private IUnityUpdateMethod updateTossHandle for the registered UpdateToss per-frame callback, or null.

Methods​

MemberWhat it is
override void Initialize()Sets up the spin sync message and remembers the rigidbody's max angular velocity.
override void InitializeAsServer(SpawnData spawnData)Server: subscribes to the pickup's grab and release events.
7 private and internal methods
MemberWhat it is
private void InteractionEnded(Interactable interactable, Interactor interactor)Server: on release, starts watching the toss if it is moving up fast enough and not too far sideways.
private void InteractionStarted(Interactable interactable, Interactor interactor)Server: stops watching for a toss when the object is grabbed.
private void OnCollisionEnter(Collision collision)Stops watching for a toss when the object hits something.
private void OnDisable()Unsubscribes from the pickup events.
private IEnumerator Spin(Vector3 angularVelocity) (iterator)Coroutine: plays particles, waits spinDelay, applies the spin and an upward boost, then restores the angular velocity cap after 5 seconds.
private void Toss(IPlayer player, Stream stream, Vector3 angularVelocity)Sync handler: serializes the spin direction and starts Spin on the receiving side.
private void UpdateToss()Server, each frame: once upward speed drops below peakMaximum, picks a random spin, sends it to chunks and spins.