Changing animation of the hearts of the symbolic lives counter through the script

In this recipe, we will go even further than we went in the previous recipe. Here, we will see how we can control the Animator controller through a script. In this specific case, you will learn how to change the speed of the controller according to the number of lives that the player has left. In fact, we want to make the hearts beat at a normal speed when the player has all his lives intact and beat faster when the player has fewer lives.

Getting ready...

In order to get started, you should have completed the previous recipe, Animating hearts of the symbolic lives counter, because you need to have all the hearts animated through an Animator controller, called HeartAnimationController.

So first of all, we need to be sure that all the hearts have an Animator component attached along with the Controller variable filled by HeartAnimationController.

Since we need to change the Animator component through the script, we can do this directly inside SymbolicLivesCounterScript. We had written it in the second chapter in the Creating a symbolic lives counter recipe. You might remember that it handles all of the logic for the lives counter.

Tip

If we don't want to override the script and keep the original one as well, we can rename it SymbolicLivesCounterAnimatedScript. Also remember to rename the class inside the script with the same name.

How to do it...

  1. For the first step, double-click on SymbolicLivesCounterAnimatedScript to open it.
  2. Next, go through it inside the updateSymbolicLivesCounter() function. Since this function in called every time a heart is lost or gained, it is the perfect object for making a general animation over all the hearts of the lives counter.
  3. In particular, we need to go inside the for cycle, immediately after the hearts[i].SetActive(true); instruction. Since we are dealing with all the active hearts here, we need to change the animation to only these, and not to the other ones that are not on the screen anymore.
  4. In this context, hearts[i] is an active heart, and we need to get its Animator component through the GetComponent<Animator>() function. Then we set the speed to hearts.Length - lives. So, let's add it to our code:
      hearts [i].GetComponent<Animator> ().speed = hearts.Length - lives;
  5. Finally, we just save our script, and click on play to test it.

How it works...

We start by getting the Animator of each active heart, and then we want to see them beating according to the number of lives the player has left. Therefore, the speed of the animation can be calculated as the difference between the maximum number of lives, which in this case is the length of the array, and the remaining lives. In fact, this difference gives us a bigger number with each life that is lost.

There's more...

In this recipe, we saw how to animate the lives counter differently through script, but maybe we need more control over these animations.

Adding a speed controller to customize speed in the Inspector and at runtime

Right now, our script uses just the difference to calculate the animation speed for each heart. If we want to tweak this speed in some way with a parameter but keep it dependent on the number of lives the player has left, we can add a new public variable to our script:

public float speedController = 1f;

As we can see, the default value is 1f. This means that there is no difference in speed compared with before, since this variable will be multiplied.

Now, change the only line of code added in this recipe in this way:

hearts [i].GetComponent<Animator> ().speed = (hearts.Length - lives)*speedController;

By multiplying the difference of our parameters, we are able to control it through the Inspector, or even during runtime if we change this variable through another script, since it is a public variable. If speedController stores a number between 0 and 1, the animation speed is reduced. Otherwise, if it is greater than 1, the animation speed is increased.

Customizing each animation

On the other hand, if we want to customize each animation, we should change the code and write a long series of if statements. How to change the animation is entirely up to you. For instance, if we want to change only the first heart, we can put it inside the updateSymbolicLivesCounter() function outside the for cycle with these lines:

if (lives == 1) {
  hearts [0].GetComponent<Animator> ().speed = 2.5f;
} else {
  hearts [0].GetComponent<Animator> ().speed = 1.0f;
}
..................Content has been hidden....................

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