Before moving ahead, I want to give a quick rundown of what we are doing. We have an object with two animation states: idle and moving. Idle is the default mode. We have created an idle parameter with the checkbox checked, which makes the condition true.
In the next section, we will write a script that has the following two main functions:
- If the object is selected, it changes idle from true to false or vice versa
- In the update function, it checks for two conditions: whether idle is true or false and to play the correct animation
When the application loads, you will see the two boxes; because idle is the default state, nothing will happen until they are selected. Once they are, they will move. If they are selected again, they will go idle once more:
- With LeftRect selected in the Hierarchy view, search for the Inspector panel, scroll to the bottom, and click on Add Component:
- Scroll down to New Script and select it.
- Name the new script RectAnim:
- Click on Create and Add.
For the RightRect object, we do not have to create a new script; we will use the same one. Since we gave the states for both RightRect and LeftRect the same names, it will allow this to work with the same script:
- Select the RightRect object.
- Click on Add Component in the Inspector view.
- Click on Scripts and select RectAnim(Script).
This is what it should look like in the Inspector for both the objects when done: