美文网首页
Survival Shooter Tutorial中文解析(三)

Survival Shooter Tutorial中文解析(三)

作者: Lv1_Sans | 来源:发表于2017-03-17 16:53 被阅读0次

    PHASE FIVE

    1. Sacen视图中选择2D模式,菜单栏选择GameObject→UI→Canvas,重命名Canvas为HUDCanvas,添加组件Canvas Group,不勾选Interactable和Blocks Raycasts;
    2. 为HUDCanvas添加空的游戏对象命名为HealthUI,通过Anchor Presets,按住Alt和Shift将它的位置和中心点设到左下角,Wdith设为75,Height设为60;
    3. 右键HealthUI→UI→Image添加图像命名为Heart,设置Width和Height为30,在Heart的Image组件中从源图片中选择Heart;
    4. 右键HealthUI→UI→Slider添加条形命名为HealthSlider,设置Position X为95、Y为0,展开HealthSlider,删掉其子对象Handle Slide Area;
    5. 在HealthSlider的Slider组件中,将Transition模式设为None,MaxValue设为100,拖动滑动条Value到100;
    6. 右键点击HUDCanvas,创建UI→Image,重命名为DamageImage,设置Rect Transform,Alt右键点击右下角的Stretch将其尺寸扩展为全屏大小,在Image组件中,Colour块中Alpha(A)设为0。

    PHASE SIX

    1. 为Player添加脚本PlayerHealth,拖动Hierarchy中的HealthSlider及DamageImage到脚本中的相应位置,为脚本中的Death Clip添加音效Player Death;
    2. 为Zombunny添加脚本EnemyAttack。
    脚本PlayerHealth解析
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    using UnityEngine.SceneManagement;
    
    namespace CompleteProject
    {
        public class PlayerHealth : MonoBehaviour
        {
            public int startingHealth = 100;//Player的初始血量100
            public int currentHealth; //定义Player的当前血量变量
            public Slider healthSlider; //定义引用血条参数
            public Image damageImage; //引用受伤时的图像                        
            public AudioClip deathClip;//添加角色死亡的音效
            public float flashSpeed = 5f;//设定damageImage的淡出屏幕的速度
            public Color flashColour = new Color(1f, 0f, 0f, 0.1f); //设定damageImage的颜色
    
    
            Animator anim;//引用动画组件参数
            AudioSource playerAudio;//引用音效组件参数
            PlayerMovement playerMovement;//引用脚本PlayerMovement
            PlayerShooting playerShooting;//引用脚本PlayerShooting
            bool isDead; //声明bool值判断角色是否死亡
            bool damaged;//声明bool值判断角色是否受伤
    
    
            void Awake ()
            {
                // Setting up the references.
                anim = GetComponent <Animator> ();//设置动画
                playerAudio = GetComponent <AudioSource> ();//设置音效
                playerMovement = GetComponent <PlayerMovement> ();//指定脚本PlayerMovement
                playerShooting = GetComponentInChildren <PlayerShooting> ();//指定脚本PlayerMovement
    
                currentHealth = startingHealth;//将角色初始生命设置为当前生命值
            }
    
    
            void Update ()
            {
                if(damaged)
                {
            
                    damageImage.color = flashColour;//角色受伤时,将flashColour赋值给damageImage的color;
                }
                else
                {
                    damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);//通过Lerp,将受伤后屏幕显示的颜色渐变消除
                }
                damaged = false;//重置角色的受伤状态为否;
            }
    
    
            public void TakeDamage (int amount)
            {
                damaged = true;//改变角色受伤状态为真
                currentHealth -= amount;//当前血量减少,减少值为受到的伤害值
                healthSlider.value = currentHealth;//将当前血量赋值给血条
                playerAudio.Play ();//播放角色受伤的音效,引用的是Player的音效组件中的Player Hurt
    
                if(currentHealth <= 0 && !isDead)
                {
                    Death ();//当Player的血量小于等于0,isDead为flase时;
                }
            }
    
    
            void Death ()
            {
                isDead = true;//Player判定为死亡
                playerShooting.DisableEffects ();//调用playerShooting脚本中的DisableEffects方法关闭射击特效
                anim.SetTrigger ("Die");//通过触发器播放Player的死亡动画
                playerAudio.clip = deathClip;//音效组件引用脚本中的音效deathClip,Player Hurt的音效会停止播放
                playerAudio.Play ();//播放音效组件中的deathClip音效
    
                playerMovement.enabled = false;//关闭脚本PlayerMovemonet
                playerShooting.enabled = false;//关闭脚本PlayerShooting
            }
    
            public void RestartLevel ()
            {
                SceneManager.LoadScene (0);//重载当前的场景
            }
        }
    }
    
    脚本EnemyAttack解析
    using UnityEngine;
    using System.Collections;
    
    namespace CompleteProject
    {
        public class EnemyAttack : MonoBehaviour
        {
            public float timeBetweenAttacks = 0.5f;//攻击的间隔时间
            public int attackDamage = 10; //攻击的伤害值
    
            Animator anim; //声明引用动画的参数
            GameObject player; //声明引用游戏对象的参数
            PlayerHealth playerHealth;//声明引用脚本PlayerHealth的参数
            EnemyHealth enemyHealth; //声明引用脚本EnemyHealth的参数
            bool playerInRange;//定义角色是否在被攻击范围的布尔值
            float timer;//定义下次攻击的计时器  
    
            void Awake ()
            {
                // Setting up the references.
                player = GameObject.FindGameObjectWithTag ("Player");//通过标签设置引用的游戏对象为Player
                playerHealth = player.GetComponent <PlayerHealth> ();//引用脚本PlayerHealth
                enemyHealth = GetComponent<EnemyHealth>();//引用脚本EnemyHealth
                anim = GetComponent <Animator> ();//引用游戏动画组件
            }
    
            void OnTriggerEnter (Collider other)
            {
                if(other.gameObject == player) //如果进入碰撞范围的是Player
                {
                   playerInRange = true;//那么Player在攻击范围内
                }
            }
    
    
            void OnTriggerExit (Collider other)
            {
    
                if(other.gameObject == player)//如果退出碰撞范围的是Player
                {
                    playerInRange = false;//Player不在攻击范围内了
                }
            }
    
    
            void Update ()
            {
                // Add the time since Update was last called to the timer.
                timer += Time.deltaTime;//每执行一次Update,timer计时器增加一次deltaTime
    
                // If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
                if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)//如果计时器超过了攻击间隔时间 且 玩家在被攻击范围内 且 怪物当前生命大于0
                {
                    Attack ();//怪物进行一次攻击
                }
    
                if(playerHealth.currentHealth <= 0)//如果Player的生命值小于或等于0
                {
                    anim.SetTrigger ("PlayerDead");//Player的动画触发器“PlayerDead”启动,播放该动画
                }
            }
    
            void Attack ()
            {
                timer = 0f;//每次执行攻击方法时,重置计时器
                if(playerHealth.currentHealth > 0)//如果Player的生命值大于0
                {
                    playerHealth.TakeDamage (attackDamage);//怪物对Player造成伤害
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Survival Shooter Tutorial中文解析(三)

          本文链接:https://www.haomeiwen.com/subject/sqoenttx.html