怪物的生成 攻擊和掉落金幣


       今天做了一天 做出來了怪物的攻擊死亡動畫和掉落金幣  代碼如下:

      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);

                }

            }

            else 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);//死後產生金幣

                //經驗增加

            }

        }

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章