Unity-3D塔防

技术点:

1. UGUI   事件的绑定   动画的制作

2. 创建preFab对象  

3. 类对象的封装

4. 泛型集合List

实现效果:

Unity-3D塔防_第1张图片

在整个游戏开发过程中,游戏设计思路很重要,实现的方法其次,先是搭建游戏的场景,场景搭建完毕之后,创建敌人的预设和你的炮塔的预设,在这里面需要用到泛型集合List,以及封装类对象,产生类的对象集合,

封装类的对象  public Wave[] waves;

根据敌人的预设,不断产生敌人按照每波敌人产生,中间设置等待时间。

void Start() {

        StartCoroutine(SpawnEnemy());

    }

 

    IEnumerator SpawnEnemy() {

      foreach(Wave wave in waves ){

          for (int i=0;i

          {

              GameObject.Instantiate(wave.enemyPreFab,START.position,Quaternion.identity);

              EnemyCount++;

              yield return new WaitForSeconds(wave.rate);

          }

          while (EnemyCount>0)

          {

              yield return 0;

          }

          yield return new WaitForSeconds(waitTime);

      }

}

等待敌人产生之后  设置敌人的运动轨迹,通过不带的设置坐标  实现敌人的轨迹行走

void Move() {

        if (index > positions.Length - 1)

            return;

        transform.Translate((positions[index].position-transform.position).normalized*Time.deltaTime*speed);

        if(Vector3.Distance(positions[index].position,transform.position)<0.2f)

        {

            index++;        

        }

        if (index > positions.Length - 1)

        {

            RechDestination();

        }

}

当敌人走到终点时   销毁敌人

void RechDestination() {

        GameObject.Destroy(this.gameObject);

}

 

根据初始金钱去建造炮塔   

判断当前点击的地,是否存在炮塔,和金钱是否够用,将预设炮塔实例化生成在场景中,

void Update() {

        if (SelectTurrentData != null&&Input.GetMouseButtonDown(0))

         {

          //¨??Ì?¡ÂÌ??º?UI

          if(EventSystem.current.IsPointerOverGameObject()==false)

          {

              //¨²¬¡§Ì?¡§¨¬

              Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

              RaycastHit hit;

              //¨¦°?º?¨£¤¨¤¤?¡À¨®¤¡é¦?¦?线?

              bool istrueet=Physics.Raycast(ray, out hit,1000, LayerMask.GetMask("MapCube"));

              if (istrueet)

              {

                  //?¨?Ì¡À¡ãÌ?¡ÂÌ?¨¦

                  MapCube mapCube = hit.collider.GetComponent<MapCube>();

                  if (SelectTurrentData != null && mapCube.turrentGo == null)

                  {

                      //¨¦°?ä¡ä¡§

                      //¨´ä?D??º?¤??

                      if (money >= SelectTurrentData.cost)

                      {

                          ChangeMoney(-SelectTurrentData.cost);

                          mapCube.BuildTurrent(SelectTurrentData.TurrentPrefab);  

                      }

                      else

                      {

                         //???

                          moneyAnimator.SetTrigger("MoneyTrigger");

                      }

 

                  }

                  else if (mapCube.turrentGo != null)

                  {

                      //Á?¦y?ä|¤¨ª

                      

                      //if (mapCube.isUpdreged)

                      //{

                      //    showUpgragededUI(mapCube.transform.position, true);

                      //}

                      //else

                      //{

                      //    showUpgragededUI(mapCube.transform.position,false);

                      //}

                      if (mapCube.turrentGo == selectTurrentGo && updgerdedcanvas.activeInHierarchy)

                      {

                          hideUpgragededUI();

                      }

                      else

                      {

                          showUpgragededUI(mapCube.transform.position, mapCube.isUpdreged);

                      }

                      selectTurrentGo = mapCube.turrentGo;

 

                  }

              }

          }

      }

}

炮塔攻击,给炮塔添加碰撞体,敌人进入碰撞体,开始创建子弹,目标点敌人,

void Attack() {

        //T?BUG   ?À¨º¨²¨´  ?¨²ä¡ä¨¬Á¨®Ì¡¥

        if(enemys[0]==null)

        {

            UpdateEnemys();

        }

        if (enemys.Count > 0)

        {

            GetComponent<AudioSource>().Play();

            GameObject Bullit = GameObject.Instantiate(builltPreFab, firePosition.position, firePosition.rotation);

            Bullit.GetComponent<Built>().SetTarget(enemys[0].transform);

        }

        else

        {

            timer = attackRateTime;

        }

    }

子弹面朝敌人运动

  void Update() {

        if (target== null)

        {

            Destroy(this.gameObject);

            return;

        }

        transform.LookAt(target.position);

        transform.Translate(Vector3.forward * speed * Time.deltaTime);

        Vector3 dir = target.position - this.transform.position;

        if (dir.magnitude < 1.3f)

        {

            target.GetComponent<Enemy1>().TakeDamage(damage);

            Die();

        }

       

    }

子弹碰撞到敌人,敌人呢掉血,子弹消失

void Die() {

        GameObject effect = GameObject.Instantiate(explosionEffict, transform.position, transform.rotation);

        Destroy(effect,1);

        Destroy(this.gameObject);

    }

敌人开始掉血

  public void TakeDamage(int damage) {

        if (hp <= 0)

            return;

        hp -= damage;

        silderHp.value = (float)hp / totleHp;

        if(hp<=0)

        {

            Die();

        }

       

    }

    void Die() {

 

        BuildManger._instance.addMoney(money);

        Destroy(this.gameObject);

        GameObject exp=GameObject.Instantiate(expliseenemy,transform.position,transform.rotation);

        Destroy(exp,1);

    }

血量为0时死亡。

 



你可能感兴趣的:(3d,人物,mybatis,技术,程序员)