wobblecoin™ documentation
Quick Start
Installing the package:
Import the package into Unity. It will appear in Assets/wobblewares/Coin. Please note that for URP support, you must install the unitypackage located inside Assets/wobblewares/Coin/
Drag the Coin from the Assets/wobblewares/Coin/Core/Prefabs folder into the scene.
(Optional) Drag separate designs from the Assets/wobblewares/Coin/Core/Prefabs/Designs folder under the Coin/Mesh transform. To apply the design to the top of the coin, zero out the transforms. To apply the design to the bottom of the coin, set the x rotation to 180 degrees.
Tweak FlipHeight, FlipDuration and RotationCount in the Coin's inspector as desired
Call the Flip( targetPosition ) function via script to flip the coin.
Installing Samples:
The samples are located in Assets/wobblewares/Coin/Samples. Note that the samples utilise wobblekit (a free lightweight vfx package). Ensure that you also unpack the URP.unitypackage inside Assets/wobblewares/Kit/ for URP support.
Also, switch to Linear colour space for best visual results!
Compatibility
Unity Version & Platforms
Tested in Unity 2022.3.1 LTS on Mac but is simple enough to work on most recent Unity versions on all platforms.
Render Pipeline Compatibility
The asset is provided with basic materials, prefabs and sample scenes for both the Built-in pipeline and the URP. Unpack the URP.unitypackage located inside Assets/wobblewares/Coin/Core for URP support.
Wobbly toon materials and vfx have also been provided for both pipelines through the included package 'wobblekit'. Please unpack the URP.unitypackage located inside Assets/wobblewares/Kit/Core for URP sample support.
No Dependencies
No external dependencies are necessary. The Coin prefab can be dropped into a vanilla unity scene with no problems.
Configuration
Hierarchy
The asset consists of a root Coin object and two sub objects, the Mesh and Colliders
Mesh
Parent of the visual mesh. This can be replaced with your own mesh but ensure you build or rotate your mesh so that 'heads' is facing upwards. Designs (eg. skull, star) are provided as separate objects that can be nested under the Coin transform.
Colliders
Convex MeshCollider used for collisions. This can be replaced with your own collider (eg. a BoxCollider approximating the coin shape)
Inspector
Coin.cs
The core component used for all coin behaviour.
Rigidbody
The standard Unity Rigidbody used for physics movement and interaction with the environment.
Audio Source
The audio source used to play the Flip & Land sounds.
Usage
How do I flip the coin?
There are two ways to flip the coin. You can flip it to a specific target location, or you can specify a direction and distance instead.
Flip ( targetPosition ) - flip directly to the target position
Flip ( direction, distance ) - flip distance units in the specified direction
The height and duration of the flip is always determined based on the inspector variables FlipHeight & FlipDuration.
You can also specify a target side as an optional argument. If no target side is set, a side will be randomly determined using HeadsWeight.
How do I check the result of a coin flip?
Either listen to the OnFlipComplete event or check Side at any point to determine which side is facing upwards.
I hate the toon wobble shader. How do I turn it off?
The samples use a custom wobble toon shader, but you can easily replace the material (and even the entire asset) with whatever standard material you would like. I would suggest finding a cheap / free coin on itch.io that suits your game!
Where can I use this asset?
Once purchased, you can use and edit this asset for any personal or commercial project - but you can't resell it directly, even if it has been heavily modified. Contact ian@wobblewares.com if you need more information.
Functionality
Flip directly to a target position or in a specific direction
Stop a flip in mid-air
Get current coin state and visible side
Calculate a random result between heads & tails based on set weighting (can be used without flipping the actual coin)
Various events triggered at key points of the Coin flip
OnFlipStart - invoked at the start of the flip
OnFlipEnd - invoked at the end of the flip but before the coin has settled on a result
OnSettled<Side> - invoked once the coin stops moving after a flip. Returns the visible side
OnApexReached - invokes at the apex of the coin flip, before the coin begins descending
OnCollide - invokes when the coin collides with an object during the flip
Landing on Heads or Tails
The coin result (heads or tails) is determined based on the side of the coin that is closely aligned with the world up vector. Checking Side of the coin will dynamically return Heads or Tails based on the side facing upwards.
You can specify a targetSide when using either of the Flip functions to force the coin to land with a particular side facing upwards. Providing no target side will randomly determine a side based on the HeadsWeight set on the coin.
Trajectory
The trajectory is calculated using FlipHeight and FlipDuration. It is split into an ascending step, and a descending step and height differences between the start position and the target position are factored in to provide a constant arc through the air.
Physics Movement & Collisions
The coin is moved via Rigidbody MovePosition and MoveRotation which should correctly interact with the physics system but is not driven by forces. This allows a deterministic result when you toss the coin - and ensures the coin lands flat on the surface. A quirk of this approach is that gravity is turned off at the start of the coin toss, and turned back on when the toss is complete.
OnCollisionEnter/Stay/Exit functionality should work as expected but the force applied to other objects when hit won't always seems natural.
Events & State
At any point, State can be checked on the coin to determine if the coin is Idle, Flipped or Settling. The settling state is used once the coin toss has completed descending, but the coin is still falling or moving. Once the coin has settled, the OnFlipComplete event will execute.
You can listen to various other events during coin usage. Subscribe to OnFlipped, OnCollide and OnApexReached to run code when certain things occur during the coin's toss.
VFX & SFX
Coin Material
The Coin prefab uses a Standard Unity material with basic uv unwrapping and has a single diffuse texture. A UV layout texture is also provided with the package to use as reference to create your own designs.
Heads & Tails designs
The Heads and Tails designs (eg. skull, star, etc) are included as separate objects. Swap them out for whatever symbol meshes you like - just make sure you put heads on the top of the coin, and tails on the bottom.
Sound FX
Custom sound effects have been created for coin toss and collision. Swap them out in the Coin inspector.
License
You can use this asset in personal or commercial projects
You can edit the assets freely, but cannot repackage, resell or redistribute them in any way.
No credit required, but you can provide a link to https://wobblewares.com if you want!