Here is the Game Plan

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:

  1. With LeftRect selected in the Hierarchy view, search for the Inspector panel, scroll to the bottom, and click on Add Component:
  1. Scroll down to New Script and select it.
  2. Name the new script RectAnim:
  1. 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:

  1. Select the RightRect object.
  2. Click on Add Component in the Inspector view.
  3. Click on Scripts and select RectAnim(Script).

This is what it should look like in the Inspector for both the objects when done:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.100.120