Triggering a Coin Toss Animation (third-person stealth game)

Marcus Ansley
3 min readJun 13, 2021

--

The player avatar in this third-person stealth game wasn’t previously giving much indication of having thrown a coin, so I’ve gone about addressing this using the Animator component, the ‘Any State’ animation state, and the Animator.SetTrigger method.

Objective: trigger a ‘thowing’ animation for our avatar on command.

1. To begin, hop into the Animator window while you’ve got the avatar selected (assuming the Animator component is located there).

2. Drag the ‘Throwing’ animation into this window and connect it to both the Idle and Walk Animation States. Make sure to select each of these transitions and set the appropriate condition (i.e. if the player was walking, make sure the condition to the Walk animation state is ‘Moving’/Walking, depending on what you named the bool):

This ‘Conditions’ section at the bottom is what interests us here

3. Back in the Animation Window, you might notice there are some default Animation States (created by default) such as Entry, Exit, and Any State. By creating a transition from Any State to the Throw animation state, we’re saying that regardless of which animation state we’re in this transition can be called.

4. In the left column, click the plus icon and create a new Trigger. Name this something like ‘Throw’ and set it to false/unticked by default if it isn’t already. Now we just need to select the transition between Any State and Throw and change its condition in the inspector to the ‘Throw’ trigger.

You should end up with something in the Animator window which looks a bit like this:

5. Over in the Player script, we can find an appropriate place to call this animation, which for me would be within a TossCoin method. The specific method we’ll need is the Animator.SetTrigger method (passing in “Throw” as the argument, and using our _animator variable as the Animator):

6. To prevent the player from moving while the animation is playing (and stop them from seemingly sliding along the floor), I’m calling a coroutine here called ‘PauseMovement’ and am passing in the duration of the pause. Unity makes pausing the NavMeshAgent pretty effortless, so the corotuine needed can be pretty simple:

And with that done, all I’ve yet to figure out is how I can get the player to face the coin for the duration of this pause/animation. The ‘transform.LookAt(position)’ statement in the TossCoin method temporarily faces the avatar in the right direction, but there’s still something telling it to face back in its initial direction a frame later. I’ll see if I can find the code that’s determining that 😉

--

--

Marcus Ansley
Marcus Ansley

Written by Marcus Ansley

Game Developer | Game Design and Literature Graduate

No responses yet