unity官方demo学习之Stealth(十二)角色生命值

1,为玩家添加脚本DonePlayerHealth

using UnityEngine;
using System.Collections;

public class DonePlayerHealth : MonoBehaviour
{
    public float health = 100f;							// How much health the player has left.
	public float resetAfterDeathTime = 5f;				// How much time from the player dying to the level reseting.
	public AudioClip deathClip;							// The sound effect of the player dying.
	
	
	private Animator anim;								// Reference to the animator component.
	private DonePlayerMovement playerMovement;			// Reference to the player movement script.
	private DoneHashIDs hash;							// Reference to the HashIDs.
	private DoneSceneFadeInOut sceneFadeInOut;			// Reference to the SceneFadeInOut script.
	private DoneLastPlayerSighting lastPlayerSighting;	// Reference to the LastPlayerSighting script.
	private float timer;								// A timer for counting to the reset of the         level once the player is dead.
	private bool playerDead;							// A bool to show if the player is dead or not.
	
	
	void Awake ()
	{
		// Setting up the references.
		anim = GetComponent();
		playerMovement = GetComponent();
		hash = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent();
		sceneFadeInOut = GameObject.FindGameObjectWithTag(DoneTags.fader).GetComponent();
		lastPlayerSighting = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent();
	}
	
	
    void 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();
			}
		}
	}
	
	
	void 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);
	}
	
	
	void 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();
	}
	
	
	void 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 void TakeDamage (float amount)
    {
		// Decrement the player's health by amount.
        health -= amount;
    }
}


参数:(公有)  玩家血量
    死亡间隔时间重置场景
死亡音乐剪辑


(私有)
动作控制器,引用状态
DonePlayerMovement  脚本引用停止玩家移动
DoneHashIDs脚本  使用Hash ID 
DoneSceneFadeInOut脚本  死亡后屏幕淡出
DoneLastPlayerSighting脚本  死亡后关闭警报器


timer  计算死亡后与重置之间的时间
playerDead 判断玩家是否死亡


函数:  awake,获取并设置各组件变量




Update
当玩家血量<=0时
当playerDead为false时(即玩家还没“死过”时)
调用函数PlayerDying执行玩家死亡功能
否则,当playerDead为true时,即玩家死过时
调用PlayerDead();
   LevelReset();



PlayerDying
设置playerDead为true,使这个函数在update中只被调用一次
设置动作控制器的deadBool变量也为 true,播放死亡动画
跟喊叫一样。调用AudioSource.PlayClipAtPoint播放一次死亡音效




PlayerDead
判断如果控制器获得的状态信息为dyingState时
禁用死亡动画(防止重复播放死亡动画)
设置速度为0f
禁用玩家移动脚本 (防止玩家死后继续移动)
重置玩家的最后坐标,使警报器停止
停止播放脚步声




LevelReset
玩家死后,计时器自增
判断如果计时器 >= 死亡重置时间
调用sceneFadeInOut脚本引用的EndScene函数执行场景淡入淡出重置场景




TakeDamage()公有函数
这个函数被其他脚本调用使玩家掉血

最后返回unity,Death Clip赋值,audio->endgame

你可能感兴趣的:(unity)