Survival Shooter Tutorial中文解析(四)

PHASE SEVEN

  1. 为Zombunny添加脚本EnemyHealth,为脚本中的Death Clip添加音效Zombunny death;
  2. 预设文件夹中,选择GunParticles,通过齿轮复制该组件,选中Player的子对象GunBarrelEnd,通过齿轮粘贴为新组件;
  3. 为GunBarrelEnd添加新组件Line Renderer,展开Matrials区域,选择材质LineRenderMaterial;展开Line Renderer的变量部分,设置Start Width和End Width为0.5,选择不开启组件Line Renderer;添加组件Light,设为黄色,选择不开启组件Light;添加组件Audio Sourece,设置Audio clip为Player Gunshot,不勾选Play On Awake;
  4. 为GunBarrelEnd添加脚本PlayerShooting。

PHASE EIGHT

  1. 右键点击HUDCanvas,添加Text重命名为ScoreText,设置位置为顶部中间,Position X为0、Position为-55、Width为300,Height为50,Text中输入文本“Score:0”,字体改为Luckiest Guy,字体大小为50,居中对齐,颜色改为白色;
  2. 为ScoreText添加组件Shadow,Effect Distance值设为(2,-2);
  3. 为ScoreText添加脚本ScoreManager;
  4. 保存Zombunny为预设。
脚本EnemyHealth解析
using UnityEngine;

public class EnemyHealth : MonoBehaviour
{
    public int startingHealth = 100;//敌人初始血量
    public int currentHealth;//敌人当前血量
    public float sinkSpeed = 2.5f;//敌人死亡后下沉速度
    public int scoreValue = 10;//消灭敌人的分数值
    public AudioClip deathClip;//敌人死亡音效

    Animator anim;//引用动画组件参数
    AudioSource enemyAudio;//引用音效参数
    ParticleSystem hitParticles;//引用粒子特效系统参数
    CapsuleCollider capsuleCollider;//引用胶囊体参数
    bool isDead;//敌人是否死亡布尔值
    bool isSinking;//敌人是否在下沉布尔值

    void Awake ()
    {
        anim = GetComponent  ();//获取动画组件
        enemyAudio = GetComponent  ();//获取音效组件
        hitParticles = GetComponentInChildren  ();//获取子对象粒子特效
        capsuleCollider = GetComponent  ();//获取组件胶囊体

        currentHealth = startingHealth;//当前生命为初始生命
    }

    void Update ()
    {
        if(isSinking)
        {
            transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);//下沉为真值时,使敌人向下移动
        }
    }

    public void TakeDamage (int amount, Vector3 hitPoint)
    {
        if(isDead)
            return;//死亡时,不再受伤
        enemyAudio.Play ();//播放受伤音效
        currentHealth -= amount;//当前血量减去受到的伤害值
        hitParticles.transform.position = hitPoint;//把射击点坐标赋值给粒子特效位置
        hitParticles.Play();//播放粒子特效
        if(currentHealth <= 0)
        {
            Death ();//敌人生命小于等于0时,敌人死亡
        }
    }

    void Death ()
    {
        isDead = true;//死亡布尔值为真
        capsuleCollider.isTrigger = true;//使胶囊体触发器为真
        anim.SetTrigger ("Dead");//动画触发器“Dead”启动
        enemyAudio.clip = deathClip;//将deathClip赋值给声音源
        enemyAudio.Play ();//播放deathClip
    }


    public void StartSinking ()
    {
        GetComponent  ().enabled = false;//寻路导航关闭
        GetComponent  ().isKinematic = true;//刚体受力影响为真
        isSinking = true;//下沉状态为真
        ScoreManager.score += scoreValue;//分数计数增加
        Destroy (gameObject, 2f);//2秒后摧毁当前游戏对象
    }
}
脚本PlayerShooting解析
using UnityEngine;

public class PlayerShooting : MonoBehaviour
{
    public int damagePerShot = 20;//定义每次射击的伤害值
    public float timeBetweenBullets = 0.15f;//射击间隔
    public float range = 100f;//射击范围

    float timer;//计时器,用于判定射击的时间间隔
    Ray shootRay;//射击的射线
    RaycastHit shootHit;//射击的点
    int shootableMask;//可被射击层的遮罩
    ParticleSystem gunParticles;//枪的粒子特效
    LineRenderer gunLine;//枪的组件LineRenderer
    AudioSource gunAudio;//射击音效
    Light gunLight;//射击的光照效果
    float effectsDisplayTime = 0.2f;//特效持续时间

    void Awake ()
    {
        shootableMask = LayerMask.GetMask ("Shootable");//获取可射击层
        gunParticles = GetComponent ();//获取粒子特效组件
        gunLine = GetComponent  ();//获取线渲染器组件
        gunAudio = GetComponent ();//获取音效
        gunLight = GetComponent ();
    }//获取光特效组件

    void Update ()
    {
        timer += Time.deltaTime;//tiemr计时器

        if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
        {
            Shoot ();//输入开火键 且 计时器大于射击间隔时间 且 timeScale传递时间不为0时 进行射击
        }

        if(timer >= timeBetweenBullets * effectsDisplayTime) //计时器大于等于射击间隔时间*特效显示时间时
        {
            DisableEffects ();//关闭射击特效
        }
    }

    public void DisableEffects ()
    {
        gunLine.enabled = false;//线渲染器关闭
        gunLight.enabled = false;//光照组件关闭
    }

    void Shoot ()
    {
        timer = 0f;//射击时计时器重置
        gunAudio.Play ();//播放设计音效
        gunLight.enabled = true;//光照特效开启
        gunParticles.Stop ();//粒子特效关闭
        gunParticles.Play ();//粒子特效开启
        gunLine.enabled = true;//线渲染器开启
        gunLine.SetPosition (0, transform.position);//设置线段起始点为枪所在位置

        shootRay.origin = transform.position;//射线起始点为枪的坐标
        shootRay.direction = transform.forward;//射线的方向枪的正前方

        if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))//射线与射线层内的碰撞器有交集时为真
        {
            EnemyHealth enemyHealth = shootHit.collider.GetComponent  ();//获取射击点所在碰撞器物体的组件的血量脚本
            if(enemyHealth != null)//脚本存在时
            {
                enemyHealth.TakeDamage (damagePerShot, shootHit.point);//射击点所在的敌人执行受伤的方法
            }
            gunLine.SetPosition (1, shootHit.point);//脚本不存在时,设置线段目标点为射击位置
        }
        else
        {
            gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);//射线与可射击层无交集时,设置线段目标点为为枪所在位置加上射线的长度范围
        }
    }
} 
脚本ScoreManager解析
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager : MonoBehaviour
{
    public static int score;//静态整型变量分数值
    Text text;//定义引用文本参数
    void Awake ()
    {
        text = GetComponent  ();//获取当前对象的文本组件
        score = 0;//初始化分数为0
    }

    void Update ()
    {
        text.text = "Score: " + score;显示文本值及分数的数值
    }
}

你可能感兴趣的:(Survival Shooter Tutorial中文解析(四))