您的位置:首页 > 其它

怪物的攻击 掉落金币

2015-01-15 00:19 246 查看
欢迎来到U3D社区:

       今天做了一天 做出来了怪物的攻击死亡动画和掉落金币  代码如下:

      public  static int life = 5; //怪物的生命值

      public GameObject mouse; //  定义怪物

      private NavMeshAgent agent; //定义组件

      public Transform hero;   //定义一个人物来杀死怪物

     public GameObject gold; // 定义金币

 

   void Start()

    {

        agent = GetComponent<NavMeshAgent>();//添加组件

    }

    void Update()

    {

        if (hero != null)

        {

            if (Vector3.Distance(transform.position, hero.position) < 2f)   //当人物靠近怪物2M之内时

            {

                if (life > 0)

                {

                    AnimationToWorking();  //做攻击动画

                }

                else if (life <= 0)

                {

                    AnimationToDie(); 

                   // Destroy(this.gameObject, 1);

                }

            }

            els
4000
e if (Vector3.Distance(transform.position, hero.position) > 4f)   //当人物的距离在怪物4M之外时 

            {

                AnimationToIdle();   //做停止动画

                agent.SetDestination(transform.position);

            }

            else if (Vector3.Distance(transform.position, hero.position) < 4f)   //当人物在怪物4M之内时  

            {

                AnimationToWalk();   //怪物追击人物

                agent.SetDestination(hero.position);

            }

        }

        else

        {

            AnimationToIdle();

        }

    }

    public void AnimationToWorking()

    {

        mouse.transform.animation.Play("Ratkin_1H_Heavy Smash");  //攻击动画

    }

    public void AnimationToWalk()

    {

        mouse.transform.animation.Play("Ratkin_1H_Casual_walk");  //  追击动画

    }

    public void AnimationToDie()

    {

        mouse.transform.animation.Play("Ratkin_1H_Dying_B");   //死亡动画

    }

    public void AnimationToIdle()

    {

        mouse.transform.animation.Play("Ratkin_IDLE");   //停止动画

    }

    void OnTriggerEnter(Collider other)

    {

        //被子弹打中耐力小于零死亡

        if (other.CompareTag("Bullet"))

        {

            life--;

            print(life);   //当怪物碰到标签为 Bullet时 生命值减少

            if (life <= 0)

            {

                Destroy(this.gameObject, 1);

                Instantiate(gold, transform.position, Quaternion.identity);//死后产生金币

                //经验增加

            }

        }

    }

更多精彩在http://unity.gopedu.com/forum.php
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  U3D