【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1387|回复: 0

5-5 Player Health 角色的生命值

[复制链接]
发表于 2013-10-1 23:12:24 | 显示全部楼层 |阅读模式
学习如何通过脚本来管理玩家的生命值,比如受到攻击或者死亡
PlayerHealth
C#
  1. using UnityEngine;
  2. using System.Collections;

  3. public class PlayerHealth : MonoBehaviour
  4. {
  5.         public float health = 100f;                         // 设置玩家现存的血量.
  6.         public float resetAfterDeathTime = 5f;              // 从死亡后到关卡重新开始的时间.
  7.         public AudioClip deathClip;                         // 玩家死亡播放的声音.
  8.         
  9.         
  10.         private Animator anim;                              // Animator动画控制器变量.
  11.         private PlayerMovement playerMovement;              // PlayerMovement变量.
  12.         private HashIDs hash;                               // HashIDs变量.
  13.         private SceneFadeInOut sceneFadeInOut;              // SceneFadeInOut变量.
  14.         private LastPlayerSighting lastPlayerSighting;      // LastPlayerSighting变量.
  15.         private float timer;                                // 玩家死亡计时器.
  16.         private bool playerDead;                            // 死亡标志位.
  17.         
  18.         
  19.         void Awake ()
  20.         {
  21.                 // 初始化变量.
  22.                 anim = GetComponent<Animator>();
  23.                 playerMovement = GetComponent<PlayerMovement>();
  24.                 hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
  25.                 sceneFadeInOut = GameObject.FindGameObjectWithTag(Tags.fader).GetComponent<SceneFadeInOut>();
  26.                 lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<LastPlayerSighting>();
  27.         }
  28.         
  29.         
  30.         void Update ()
  31.         {
  32.                 // 如果玩家的血小于 0...
  33.                 if(health <= 0f)
  34.                 {
  35.                         // ... ... 这个时候玩家中枪了还没死.
  36.                         if(!playerDead)
  37.                                 // ... 那就执行玩家死亡函数.
  38.                                 PlayerDying();
  39.                         else
  40.                         {
  41.                                 // 否则, 玩家已经死了则执行.
  42.                                 PlayerDead();
  43.                                 LevelReset();
  44.                         }
  45.                 }
  46.         }
  47.         
  48.         
  49.         void PlayerDying ()
  50.         {
  51.                 // 玩家还没死绝,奄奄一息,还在交党费,那就修改标志位宣布他死亡
  52.                 playerDead = true;
  53.                
  54.                 // 设置玩家死亡动画
  55.                 anim.SetBool(hash.deadBool, playerDead);
  56.                
  57.                 // 播放玩家死亡播放的声音.
  58.                 AudioSource.PlayClipAtPoint(deathClip, transform.position);
  59.         }
  60.         
  61.         
  62.         void PlayerDead ()
  63.         {
  64.                 // 判断玩家当前是不是已经播放了死亡动画,如果已经播放,则修改标志位.
  65.                 if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.dyingState)
  66.                         anim.SetBool(hash.deadBool, false);
  67.                
  68.                 // 禁止移动.
  69.                 anim.SetFloat(hash.speedFloat, 0f);
  70.                 playerMovement.enabled = false;
  71.                
  72.                 // 游戏的警告状态解除.
  73.                 lastPlayerSighting.position = lastPlayerSighting.resetPosition;
  74.                
  75.                 // 脚步声关闭.
  76.                 audio.Stop();
  77.         }
  78.         
  79.         
  80.         void LevelReset ()
  81.         {
  82.                 // 计时器.
  83.                 timer += Time.deltaTime;
  84.                
  85.                 //死亡时间满足重新开始玩的时间...
  86.                 if(timer >= resetAfterDeathTime)
  87.                         // ... 关卡重新开始.
  88.                         sceneFadeInOut.EndScene();
  89.         }
  90.         
  91.         
  92.         public void TakeDamage (float amount)
  93.         {
  94.                 // 玩家减血.
  95.                 health -= amount;
  96.         }
  97. }
复制代码
JS
  1. #pragma strict

  2. public var health : float = 100f;                           // How much health the player has left.
  3. public var resetAfterDeathTime : float = 5f;                // How much time from the player dying to the level reseting.
  4. public var deathClip : AudioClip;                           // The sound effect of the player dying.


  5. private var anim : Animator;                                // Reference to the animator component.
  6. private var playerMovement : PlayerMovement;            // Reference to the player movement script.
  7. private var hash : HashIDs;                             // Reference to the HashIDs.
  8. private var sceneFadeInOut : SceneFadeInOut;            // Reference to the SceneFadeInOut script.
  9. private var lastPlayerSighting : LastPlayerSighting;    // Reference to the LastPlayerSighting script.
  10. private var timer : float;                                  // A timer for counting to the reset of the level once the player is dead.
  11. private var playerDead : boolean;                           // A bool to show if the player is dead or not.


  12. function Awake ()
  13. {
  14.     // Setting up the references.
  15.     anim = GetComponent(Animator);
  16.     playerMovement = GetComponent(PlayerMovement);
  17.     hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(HashIDs);
  18.     sceneFadeInOut = GameObject.FindGameObjectWithTag(Tags.fader).GetComponent(SceneFadeInOut);
  19.     lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(LastPlayerSighting);
  20. }


  21. function Update ()
  22. {
  23.     // If health is less than or equal to 0...
  24.     if(health <= 0f)
  25.     {
  26.         // ... and if the player is not yet dead...
  27.         if(!playerDead)
  28.             // ... call the PlayerDying function.
  29.             PlayerDying();
  30.         else
  31.         {
  32.             // Otherwise, if the player is dead, call the PlayerDead and LevelReset functions.
  33.             PlayerDead();
  34.             LevelReset();
  35.         }
  36.     }
  37. }


  38. function PlayerDying ()
  39. {
  40.     // The player is now dead.
  41.     playerDead = true;
  42.    
  43.     // Set the animator's dead parameter to true also.
  44.     anim.SetBool(hash.deadBool, playerDead);
  45.    
  46.     // Play the dying sound effect at the player's location.
  47.     AudioSource.PlayClipAtPoint(deathClip, transform.position);
  48. }


  49. function PlayerDead ()
  50. {
  51.     // If the player is in the dying state then reset the dead parameter.
  52.     if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.dyingState)
  53.         anim.SetBool(hash.deadBool, false);
  54.    
  55.     // Disable the movement.
  56.     anim.SetFloat(hash.speedFloat, 0f);
  57.     playerMovement.enabled = false;
  58.    
  59.     // Reset the player sighting to turn off the alarms.
  60.     lastPlayerSighting.position = lastPlayerSighting.resetPosition;
  61.    
  62.     // Stop the footsteps playing.
  63.     audio.Stop();
  64. }


  65. function LevelReset ()
  66. {
  67.     // Increment the timer.
  68.     timer += Time.deltaTime;
  69.    
  70.     //If the timer is greater than or equal to the time before the level resets...
  71.     if(timer >= resetAfterDeathTime)
  72.         // ... reset the level.
  73.         sceneFadeInOut.EndScene();
  74. }


  75. public function TakeDamage (amount : float)
  76. {
  77.     // Decrement the player's health by amount.
  78.     health -= amount;
  79. }
复制代码

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-5-23 21:11

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表