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.
- Listed in Global namespace: T to X
- Inherits or implements
NetworkEntityBehaviour
Constructors
| Member | What it is |
|---|---|
TossRandom() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
14 private and internal fields
| Member | What it is |
|---|---|
private ParticleSystem androidParticles | Particle system for Android. Not used in this code. |
private float horizontalMaximum | Most horizontal speed a release can have and still count as a toss. |
private Vector3 localBaseVelocity | Serialized vector that is not used in this code. |
private float oldMaxAngularVelocity | The rigidbody's original angular velocity cap, restored after a spin. |
private ParticleSystem particles | Particle system played when the spin starts. |
private float peakMaximum | Upward speed below which the toss is near its peak and the spin is applied. |
private Pickup pickup | The pickup on this object, used to hear grabs and releases. |
private float randomVelocity | Multiplier on the random unit spin direction, giving the angular speed. |
private float spinDelay | Seconds between the spin starting (particles) and the angular velocity being applied. |
private float spinUpBoost | Extra upward speed added when the spin is applied. |
private MethodSyncStruct<Vector3> syncSpin | Sync message (SyncA) that sends the chosen spin direction to clients. |
private Rigidbody targetRigidbody | The rigidbody that is spun. |
private float tossMinimum | Upward speed a release needs to count as a toss. |
private IUnityUpdateMethod updateToss | Handle for the registered UpdateToss per-frame callback, or null. |
Methods
| Member | What 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
| Member | What 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. |