Thursday, April 28, 2016

PlayerHealth Script

PlayerHealth:


using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerHealth : MonoBehaviour
{
                public int startingHealth = 100; // The amount of health the player starts the game with.;
                public int currentHealth;        // The current health the player has.
                public Slider healthSlider;      // Reference to the UI's health bar.
                public Image damageImage;        // Reference to an image to flash on the screen on being hurt.
                public AudioClip deathClip;       // The audio clip to play when the player dies.
                public float flashSpeed = 5f;      // The speed the damageImage will fade at.
                public Color flashColour = new Color(1f, 0f, 0f, 0.1f);  // The colour the damageImage is set to, to flash.


                float healthTimer = 0.0f;
                bool healthPackShouldRespawn = false;
                public GameObject healthPack;



                Animator m_animator;               // Reference to the Animator component.
                AudioSource m_playerAudio;         // Reference to the AudioSource component.
                PlayerMovement m_playerMovement;    // Reference to the player's movement.
                PlayerShooting m_playerShooting;    // Reference to the PlayerShooting script.
                bool isDead;                         // Whether the player is dead.
                bool damaged;                       // True when the player gets damaged.
              

    void Awake ()
    {
                                // Setting up the references.
        m_animator = GetComponent <Animator> ();
        m_playerAudio = GetComponent <AudioSource> ();
        m_playerMovement = GetComponent <PlayerMovement> ();
        m_playerShooting = GetComponentInChildren <PlayerShooting> ();
       
                                // Set the initial health of the player.
                                currentHealth = startingHealth;
                                //InvokeRepeating ("PlayerHealthPack", 5,1);
    }


    void Update ()
    {
                                // If the player has just been damaged...
                                if (damaged)
                                {
                                                damageImage.color = flashColour;
                                }
                                // Otherwise...
                                else
                                {
                                                // ... transition the colour back to clear.
                                                damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
                                }
       
                                // Reset the damaged flag.
                                damaged = false;

                                if (healthPackShouldRespawn)
                                {
                                                healthTimer += Time.deltaTime;

                                                if (healthTimer > 20.0f)
                                                {
                                                                healthPack.gameObject.SetActive (true);
                                                                healthTimer = 0.0f;
                                                              

                                                }
                               
                                }

                }
               

    public void TakeDamage (int amount)
    {
                                // Set the damaged flag so the screen will flash.
                                damaged = true;

                                // Reduce the current health by the damage amount.
                                currentHealth -= amount;

                                // Set the health bar's value to the current health.
                                healthSlider.value = currentHealth;

                                // Play the hurt sound effect.
                                m_playerAudio.Play ();

                                // If the player has lost all it's health and the death flag hasn't been set yet...
                                if(currentHealth <= 0 && !isDead)
        {
            Death ();
        }
    }

                void OnTriggerEnter(Collider item)
                {
                                if(item.gameObject.tag == "PlayerHealthPack")
                                {
                                                if(currentHealth < 100)
                                                {
                                                                currentHealth = currentHealth + 10;
                                                                healthSlider.value = currentHealth;
                                                                item.gameObject.SetActive(false);
                                                                healthPackShouldRespawn = true;

                                                }
                                }
                }

               
    void Death ()
    {
                                // Set the death flag so this function won't be called again.
                                isDead = true;

                                // Turn off any remaining shooting effects.
                                m_playerShooting.DisableEffects ();

                                // Tell the animator that the player is dead.
                                m_animator.SetTrigger ("Die");

                                // Set the audiosource to play the death clip and play it (this will stop the hurt sound from playing).
                                m_playerAudio.clip = deathClip;
                                m_playerAudio.Play ();

                                // Turn off the movement and shooting scripts.
                                m_playerMovement.enabled = false;
                               m_playerShooting.enabled = false;
    }

               

    public void RestartLevel ()
    {
        Application.LoadLevel (Application.loadedLevel);
    }

}

1 comment:

  1. For future reference, it's easiest to use something like pastee.org to send code. Change the lexer dropdown to C# to make it look a little nicer, but it isn't completely necessary. Also by default the links only work for a month, so you'll have to change that option if you want them to last longer

    For example here is one that links to your code. https://pastee.org/eajuf.

    If you can edit your post you should replace the code with that link to save space

    ReplyDelete