My journey becoming a Unity game developer: Coin Distraction-Setup the Coin Toss animation for Darren

Rhett Haynes
4 min readNov 9, 2021

--

Objective: Setup the Throw animation for Darren when he tosses the coin. Also, delay the coin appearing onscreen after Darren’s Throw animation plays completely.

Coin toss instantiated and animation playing.

Click on the Player object and select the Darren 3D model game object as it has the Animator attached to it. Double-click the Darren controller to open the Animator, and drag in the Throw animation.

Make transitions to the Throw animation state from the Idle and Walk states. Over in the Parameters tab, create a new Trigger name Throw. Click on the transition arrows, turn off the Has Exit Time so we don’t have to wait for the animation to play completely, and in Conditions set the Throw to True for both transitions.

Throw trigger setup between the Idle and Walk animations in the Animator.

In the Coins script, create a new variable that will get the Player Animator name anim. Inside of the Start() function, use the anim variable to find the Player game object and get the Animator component under the Player object inside of its child game object named Darren 3D.

Then inside of the Update() function in the Raycast IF statement, we will use the anim variable to SetTrigger the Throw animation right before we instantiate the coin itself.

When we play the game, Darren throws the coin at the spot were we right-clicked. However, when we move Darren now he will stay in the Idle animation while walking, plus the Throw animation stays on indefinitely.

Darren throws coin animation plays from Idle animation, but he is stuck in the Idle animation.

Even when we toss the coin while walking, Darren stays in the Idle animation indefinitely.

Darren throws coin animation plays from Walk animation, but he is stuck in the Idle animation.

Let’s add transitions from the Throw animation state to both Idle and Walk states. For the Throw->Idle states, turn off Has Exit Time and set the Walk condition to False which will put Darren back into his default Idle animation. With the Throw->Walk states , turn off Has Exit Time as well and set the Walk condition to True if Darren needs to walk right after tossing the coin.

Walk transitions set for after the Throw animation plays.

If we look at Darren’s Throw animation playing now, we will notice that his animation plays partially and goes back to Idle. This is happening because it is immediately performing the Walk = false condition set in the Throw->Idle transition, and the Has Exit Time is not allowing the animation to play completely.

Throw animation not working properly because of Has Exit Time turned off.

Go back inside of the Throw to Idle and Walk states and turn on the Has Exit Time to play the Throw animation completely before doing something else next.

Has Exit Time turned on for the Throw->Idle animation.

Now the Throw animation will work properly allowing Darren to complete his animation without being stopped prematurely.

Throw animation working properly during the Walk animation playing.

As we see the Throw animation is working properly with the Idle animation.

Throw animation working from the Idle animation.

One final thing is that if we look at the Throw animation and the coin instantiating, the coin appears onscreen before Darren even starts his animation.

To fix this let’s create a new variable which we will use to assign the coin’s Vector3 position from the hitInfo.point position using the name hit. Then inside of the IF Raycast statements, assign the hitInfo.point position to the hit variable. After the Throw animation is triggered, we will start a coroutine name CoinTossDelay() and pass in the hit information as a parameter for the coroutine to use. This coroutine will delay the coin appearing onscreen for a couple of seconds.

Create the coroutine name CoinTossDelay and pass in a Vector3 type for the hit parameter results. Start by pausing for 2 seconds which will happen right after Darren’s Throw animation is triggered. Then instantiate the coin prefab at the right-click’s hit point without rotation. Last, play the coin sound clip at the hit point using the AudioSource PlayClipAtPoint method.

If we trigger the Throw animation, we will see Darren going through his animation completely and the coin appearing onscreen 2 seconds after his animation starts.

Coin paused for 2 seconds before landing on the floor.

Next time we will add Voice Over Instructions to tell Darren when to toss the coin.

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet