|  | 
 
| 学习如何通过脚本来管理玩家的生命值,比如受到攻击或者死亡 PlayerHealth
 C#
 JS复制代码using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour
{
        public float health = 100f;                         // 设置玩家现存的血量.
        public float resetAfterDeathTime = 5f;              // 从死亡后到关卡重新开始的时间.
        public AudioClip deathClip;                         // 玩家死亡播放的声音.
        
        
        private Animator anim;                              // Animator动画控制器变量.
        private PlayerMovement playerMovement;              // PlayerMovement变量.
        private HashIDs hash;                               // HashIDs变量.
        private SceneFadeInOut sceneFadeInOut;              // SceneFadeInOut变量.
        private LastPlayerSighting lastPlayerSighting;      // LastPlayerSighting变量.
        private float timer;                                // 玩家死亡计时器.
        private bool playerDead;                            // 死亡标志位.
        
        
        void Awake ()
        {
                // 初始化变量.
                anim = GetComponent<Animator>();
                playerMovement = GetComponent<PlayerMovement>();
                hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
                sceneFadeInOut = GameObject.FindGameObjectWithTag(Tags.fader).GetComponent<SceneFadeInOut>();
                lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<LastPlayerSighting>();
        }
        
        
        void Update ()
        {
                // 如果玩家的血小于 0...
                if(health <= 0f)
                {
                        // ... ... 这个时候玩家中枪了还没死.
                        if(!playerDead)
                                // ... 那就执行玩家死亡函数.
                                PlayerDying();
                        else
                        {
                                // 否则, 玩家已经死了则执行.
                                PlayerDead();
                                LevelReset();
                        }
                }
        }
        
        
        void PlayerDying ()
        {
                // 玩家还没死绝,奄奄一息,还在交党费,那就修改标志位宣布他死亡
                playerDead = true;
                
                // 设置玩家死亡动画
                anim.SetBool(hash.deadBool, playerDead);
                
                // 播放玩家死亡播放的声音.
                AudioSource.PlayClipAtPoint(deathClip, transform.position);
        }
        
        
        void PlayerDead ()
        {
                // 判断玩家当前是不是已经播放了死亡动画,如果已经播放,则修改标志位.
                if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.dyingState)
                        anim.SetBool(hash.deadBool, false);
                
                // 禁止移动.
                anim.SetFloat(hash.speedFloat, 0f);
                playerMovement.enabled = false;
                
                // 游戏的警告状态解除.
                lastPlayerSighting.position = lastPlayerSighting.resetPosition;
                
                // 脚步声关闭.
                audio.Stop();
        }
        
        
        void LevelReset ()
        {
                // 计时器.
                timer += Time.deltaTime;
                
                //死亡时间满足重新开始玩的时间...
                if(timer >= resetAfterDeathTime)
                        // ... 关卡重新开始.
                        sceneFadeInOut.EndScene();
        }
        
        
        public void TakeDamage (float amount)
        {
                // 玩家减血.
                health -= amount;
        }
}
复制代码#pragma strict
public var health : float = 100f;                           // How much health the player has left.
public var resetAfterDeathTime : float = 5f;                // How much time from the player dying to the level reseting.
public var deathClip : AudioClip;                           // The sound effect of the player dying.
private var anim : Animator;                                // Reference to the animator component.
private var playerMovement : PlayerMovement;            // Reference to the player movement script.
private var hash : HashIDs;                             // Reference to the HashIDs.
private var sceneFadeInOut : SceneFadeInOut;            // Reference to the SceneFadeInOut script.
private var lastPlayerSighting : LastPlayerSighting;    // Reference to the LastPlayerSighting script.
private var timer : float;                                  // A timer for counting to the reset of the level once the player is dead.
private var playerDead : boolean;                           // A bool to show if the player is dead or not.
function Awake ()
{
    // Setting up the references.
    anim = GetComponent(Animator);
    playerMovement = GetComponent(PlayerMovement);
    hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(HashIDs);
    sceneFadeInOut = GameObject.FindGameObjectWithTag(Tags.fader).GetComponent(SceneFadeInOut);
    lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(LastPlayerSighting);
}
function Update ()
{
    // If health is less than or equal to 0...
    if(health <= 0f)
    {
        // ... and if the player is not yet dead...
        if(!playerDead)
            // ... call the PlayerDying function.
            PlayerDying();
        else
        {
            // Otherwise, if the player is dead, call the PlayerDead and LevelReset functions.
            PlayerDead();
            LevelReset();
        }
    }
}
function PlayerDying ()
{
    // The player is now dead.
    playerDead = true;
    
    // Set the animator's dead parameter to true also.
    anim.SetBool(hash.deadBool, playerDead);
    
    // Play the dying sound effect at the player's location.
    AudioSource.PlayClipAtPoint(deathClip, transform.position);
}
function PlayerDead ()
{
    // If the player is in the dying state then reset the dead parameter.
    if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.dyingState)
        anim.SetBool(hash.deadBool, false);
    
    // Disable the movement.
    anim.SetFloat(hash.speedFloat, 0f);
    playerMovement.enabled = false;
    
    // Reset the player sighting to turn off the alarms.
    lastPlayerSighting.position = lastPlayerSighting.resetPosition;
    
    // Stop the footsteps playing.
    audio.Stop();
}
function LevelReset ()
{
    // Increment the timer.
    timer += Time.deltaTime;
    
    //If the timer is greater than or equal to the time before the level resets...
    if(timer >= resetAfterDeathTime)
        // ... reset the level.
        sceneFadeInOut.EndScene();
}
public function TakeDamage (amount : float)
{
    // Decrement the player's health by amount.
    health -= amount;
}
 | 
 |