Implementing  stone throw animation

While we can simply activate the stone launch at key press, spawning the stone to launch at a player's generic discreet position and toward its forward direction, we want to add some realism in this chapter and, hence, we will spawn the stone exactly in the right hand, and we will throw the stone from there, toward the forward direction:

To achieve this, we will edit the player character Animator state machine we worked on in Chapter 5, Character Animation with Unity, so let's select the Warrior_RM GameObject in the scene hierarchy and open the Animator window if it isn't already open, then, as in the screenshot above, we will add a Throw state, linked to the main default Grounded state and create the two-way transitions.

For the Throw state, we will set the goalie_throw animation clip situated in the Chapter5-6-7\Models\Characters\Warrior_Mecanim_RM folder.

To do so, select the new Throw state in the Animator window with the Warrior_RM selected, as shown in the following screenshot:

In the Inspector, you should see the animation state information. Then, drag the goalie_throw animation nested within the Warrior_final_RM @ goalie_throw.fbx file from the project view into the Inspector in the Motion slot and set the playback Speed to 1.5 (150%):

After doing this, we will switch back to the Animator window and select Grounded -> Throw transition (the white arrow connecting the two states).

Selecting the transition arrow in the Animator window will allow us to see transition options in the Inspector. To be able to drive from scripting the state change, we will:

This will instruct Animator to switch to the Throw animation state when the FightStyle parameter becomes 2

The Inspector showing the Animator states transition properly set, with the FightStyle condition set

 Observe the following line of code:

Animator.SetInt("FightStyle",2)

This line of code is in the Fight method of the ThirdPersonCustomCharacter class and will be called by our user input code that we wrote earlier. In the same way, we will select the Throw->Grounded transition and set its Exit Time  and a condition with the FightStyle  parameter with Equal to 0 (zero) for the condition value. While this series of calls might sound complicated in the implementation, it will ensure component consistency and reusability. Let's start from the two bridge methods that will be called at the right time in the animation timeline by the events we are about to set. First, we want to declare a new public Transform variable to store the right-hand bone in the player character skeleton so that we can drag the object from the Hierarchy to the component slot after saving the script; let's call it rightHandRef:

public Transform rightHandRef;

Then we want to add these two methods to the ThirdPersonCustomCharacter class and declare them public to enable the possibility of calling them from the Animator events:

        // Throw a stone from the right hand 
public void ThrowStone()
{
SendMessage("ThrowObject", rightHandRef);
}

// restore the fight style = 0, to go back to idle state
public void EndOfSlash() {
m_Fighting_Style = 0;
m_Animator.SetInteger("FightStyle", 0);
rightHandRef.GetComponent<Collider>().enabled = false;
}

As you can see, the first method will send a message to the same GameObject that is carrying the StoneLauncher component we wrote and, hence, will be successfully receiving the message. The parameter for this message will be the right-hand reference (rightHandRef) that we previously declared and assigned dragging the Transform to the component slot. We have to pass a transform parameter because the method was declared with an input variable need:

public void ThrowObject(Transform rightHandRef)

It was declared public for your commodity because you might want to call this method from another class, such as the following:

GetComponent<StoneLauncher>().ThrowObject(spawnPoint);

At this point, the code is ready. How will we set up Animator to fire these two methods on a certain animation frame?