您的位置:首页 > 移动开发 > Unity3D

Unity学习笔记七 - Survival Shooter Tutorial

2016-04-25 20:41 393 查看
下面来看下EnemyHealth.cs脚本中的内容
    void Awake ()
    {
        anim = GetComponent <Animator> ();
        enemyAudio = GetComponent <AudioSource> ();
        hitParticles = GetComponentInChildren <ParticleSystem> ();
        capsuleCollider = GetComponent <CapsuleCollider> ();

        currentHealth = startingHealth;
    }

    void Update ()
    {
        if(isSinking)  //变量isSinking为true
        {
            transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
//使怪物的坐标逐渐下移,就是消失在地板下
        }
    }

    public void TakeDamage (int amount, Vector3 hitPoint)
    {
        if(isDead)  //如果isDead为true,就是已经置位,不执行后面的
            return;

        enemyAudio.Play ();
//播放enemy上的AudioSource组件中的音乐

        currentHealth -= amount;//当前血量减少amount
            
        hitParticles.transform.position = hitPoint; 
//enemy上的hitParticles粒子效果位置设定在光束击中的点,根据前面的代码
        hitParticles.Play(); //播放粒子效果

        if(currentHealth <= 0) 
//当前血量小于等于0,执行Death函数
        {
            Death ();
        }
    }

    void Death ()
    {
        isDead = true; 
//设置isDead = true,不用再死了

        capsuleCollider.isTrigger = true;
//本enemy上的capsuleCollider IsTrigger 设置为true  (1)

        anim.SetTrigger ("Dead"); 
//触发本enemy的animator的组件

        enemyAudio.clip = deathClip;
//设置本object的音乐片段为deathClip=死亡音乐片段
        enemyAudio.Play ();
//播放音乐
    }

    public void StartSinking ()
    {
        GetComponent <NavMeshAgent> ().enabled = false;
//禁用NavMeshAgent组件,这是一个地图路径寻址导航的Navigation的执行单元,就是其可在Navigation制作和设定好的地图路线上寻路
        GetComponent <Rigidbody> ().isKinematic = true;
//设置刚体组件的isKinematic为true 
(2)
        isSinking = true;
//把isSinking设为true,enemy可以通过transform.Translate强行下移了
        ScoreManager.score += scoreValue;
//分数记录脚本ScoreManager的score,被声明为public static,分数记录脚本ScoreManager的score,被声明为public
static,作为类的不分实体而存在的变量,有独立的存储空间,不论类被new了几次,它的存储空间都不变
        Destroy (gameObject, 2f);
//两秒钟后销毁此enemy

    }

我在百度网盘上传的工程的一个bug,大象类怪物不会执行动画和下沉Animator设置有错





Prefabs->Hellephant中Animator组件的Controller选择为HellephantAOC,Animator override Controller,覆盖类,继承EnemyAnimatorController,原始是Hellephant的Move、Idle、Death覆盖的是zombunny的对应动画,而Animator中的转换关系和触发变量不变

附加TIPs:

1.我们如果注释掉(1),(2)两行,enemy还是继续下沉,说明 transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);使武器强行移动

2.如果我们注释掉 transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);,e
af5d
nemy不下沉,只是2秒后会消失
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息