官术网_书友最值得收藏!

Creating a Health component

Both the player spaceship and enemies need health. Health is a measure of a character's presence and legitimacy in the scene, typically scored as a value between 0-100. 0 means death, and 100 means full health. Health is, in many respects, specific to each instance: the player has a unique health rating, and the enemy theirs. There are nevertheless many things in common, in terms of behavior, between player and enemy health, that it makes sense to code health as a separate component and class that can be attached to all objects that need health. Create a new class called Health, which should be attached to the player and all enemies or objects that need health:

public class Health : MonoBehaviour

{

   public GameObject DeathParticlesPrefab = null;

   public bool ShouldDestroyOnDeath = true;

   [SerializeField] private float _HealthPoints = 100f;

}

The Health class maintains object health through a private variable, _HealthPoints. The _HealthPoints variable is declared as a SerializedField, allowing its value to be visible in the Inspector while maintaining a private scope, in other words, not accessible by other scripts. The prefab variable, on the other hand, is public, allowing its value to be both seen in the Inspector and changeable from elsewhere in the code if needed.

We want to be able to change the _HealthPoints variable from another script and have some logic to check for when it reaches zero. We could create a setter function to accomplish this, but instead, we'll make use of C# properties:

public class Health : MonoBehaviour

{

    public float HealthPoints

    {

        Get         {             return _HealthPoints;         }

        Set

        {

            _HealthPoints = value;

            if(HealthPoints <= 0)

            {

                SendMessage("Die",                  SendMessageOptions.DontRequireReceiver);

                if(DeathParticlesPrefab != null)

                {

         Instantiate(DeathParticlesPrefab,

                      transform.position, transform.rotation);

                }

                if(ShouldDestroyOnDeath)                 {                    Destroy(gameObject);

                }

            }

         }

    }

}

Let's summarize the preceding code:

  • _HealthPoints is now accessible through a C# property, HealthPoints. This property features both get and set accessors to return and set the health variable.
  • The Health class is an example of event-driven programming. The class could have continually checked the health of the object in an Update function. Instead, the check for death is made during the C# property set method. This makes sense because the properties mutator is the only place where health will ever change. Not having to check the health of every object on every frame saves a lot of work. Imagine if you had thousands of objects on screen at once, and each one was constantly checking its own health! You'd see a significant performance increase by writing event-driven code.
  • The Health class uses the SendMessage function that lets you call any other function on any component attached to the object by specifying the function name as a string. In this case, a function called Die will be executed on every component attached to the object (if such a function exists). If no function of a matching name exists, then nothing happens for that component. This is a quick and easy way to run customized behavior on an object in a type-agnostic way without using polymorphism. The disadvantage of SendMessage is that it internally uses a process called Reflection, which is slow and performance-prohibitive. For this reason, SendMessage should be used infrequently. More information can be found online at https://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html.

When the health script is attached to the player spaceship, it appears as a component in the Inspector. As shown in Figure 3.17, it contains a field for Death Particles Prefab:

Figure 3.17 – Attaching the health script

Figure 3.17 – Attaching the health script

This field is optional (it can be null), and is used to specify a particle system to be instantiated when the health of the object reaches zero. This will let us instantiate an explosion particle effect when the player dies. But first, we have to create one.

主站蜘蛛池模板: 蕉岭县| 铜陵市| 伊宁市| 平湖市| 麟游县| 兴业县| 汶川县| 龙川县| 永新县| 开远市| 瑞昌市| 通州区| 西城区| 罗江县| 大方县| 龙泉市| 利川市| 沙坪坝区| 万全县| 永胜县| 大渡口区| 墨玉县| 紫阳县| 昆明市| 马边| 桑植县| 慈利县| 平泉县| 津市市| 扎囊县| 肥城市| 峨山| 信阳市| 衡水市| 越西县| 西乌| 呼玛县| 长顺县| 漠河县| 湘潭市| 甘肃省|