zoukankan      html  css  js  c++  java
  • 塔防游戏 Day1

    1. 创建地图

      

      如上图所示,白色方块为可以放置塔楼的区域,粉红色区域为敌人的行走区域,绿色方块处为起始点,黄色方块点为终止点。

    2. 控制游戏的视野

     1 public class ViewScript : MonoBehaviour {
     2     public int speed = 10;                                  // 方位键灵敏度
     3     public int mouseSpeed = 600;                            // 鼠标中键灵敏度
     4 
     5     // Update is called once per frame
     6     // W,A,S,D控制画面左右移动,鼠标中轴控制画面缩放
     7     void Update () {
     8         float h = Input.GetAxis("Horizontal");              // 获取左右键输入
     9         float v = Input.GetAxis("Vertical");                // 获取上下键输入
    10         float mouse = Input.GetAxis("Mouse ScrollWheel");   // 获取鼠标中轴输入
    11         // 摄像头移动
    12         transform.Translate(new Vector3(h * speed, mouse * mouseSpeed, v * speed) * Time.deltaTime, Space.World);
    13     }
    14 }

    3. 敌人路径管理

       在地图上每个转折点设置标记,用以表示敌人的运动路径。并设置 WayPoints 类存储路径结点。

     1 public class WayPoints : MonoBehaviour {
     2     // 存储路径结点方位
     3     public static Transform[] positions;
     4 
     5     void Awake()
     6     {
     7         positions = new Transform[transform.childCount];    // 初始化路径数组
     8         for (int i = 0; i < positions.Length; ++i)
     9         {
    10             positions[i] = transform.GetChild(i);           // 存储所有路径结点方位
    11         }
    12     }
    13 }

    4. 创建敌人,控制敌人移动

     1 public class Enemy : MonoBehaviour {
     2     private Transform[] points;             // 存储敌人路径 
     3     private int index = 0;                  // 存储当前目的地结点序号
     4     public int speed = 10;                  // 敌人速度
     5 
     6     // Use this for initialization
     7     void Start () {
     8         points = WayPoints.positions;       // 获取路径
     9     }
    10     
    11     // Update is called once per frame
    12     void Update () {
    13         Move();                             // 控制敌人移动
    14     }
    15 
    16     void Move()
    17     {
    18         // 敌人移动
    19         transform.Translate((points[index].position - transform.position).normalized * Time.deltaTime * speed);
    20         // 若到达目的地,则更新目的地
    21         if (Vector3.Distance(points[index].position, transform.position) < 0.2f)
    22         {
    23             if (index < points.Length - 1)
    24             {
    25                 index++;
    26             }
    27             else
    28             {
    29                 reachDis();                 // 到达终点
    30             }
    31         }
    32     }
    33 
    34     void reachDis()
    35     {
    36         // 到达终点后销毁敌人
    37         GameObject.Destroy(this.gameObject);
    38     }
    39 
    40     void OnDestroy()
    41     {
    42         EnemySpawner.countEnemyAlive--;     // 存活敌人个数-1
    43     }
    44 }

     

    5. 创建敌人孵化器管理敌人的生成

    1 // 自定义每波敌人的参数
    2 [System.Serializable]                   // 序列化
    3 public class Wave
    4 {
    5     public GameObject enemyPrefab;      // 敌人模型
    6     public int count;                   // 敌人数量
    7     public float rate;                  // 敌人生成间隔
    8 }
     1 public class EnemySpawner : MonoBehaviour {
     2     public Wave[] waves;                            // 定义每波敌人参数
     3     public Transform start;                         // 敌人出生点
     4     public int waveRate = 3;                        // 每波敌人生成间隔
     5     public static int countEnemyAlive = 0;          // 生存敌人个数
     6 
     7     // Use this for initialization
     8     void Start () {
     9         StartCoroutine(SpawnEnemy());               // 启动线程
    10     }
    11 
    12     IEnumerator SpawnEnemy()
    13     {
    14         foreach(Wave wave in waves)                 // 遍历每一波敌人
    15         {
    16             for (int i = 0; i < wave.count; ++i)    // 生成每个敌人
    17             {
    18                 // 生成敌人
    19                 GameObject.Instantiate(wave.enemyPrefab, start.position, Quaternion.identity);
    20                 countEnemyAlive++;                  // 生存敌人个数+1
    21                 if (i != wave.count - 1)            // 敌人生成间隔
    22                     yield return new WaitForSeconds(wave.rate);
    23             }
    24             while (countEnemyAlive > 0)             // 上一波敌人全部死亡后,才能生成下一波敌人
    25             {
    26                 yield return 0;
    27             }
    28             yield return new WaitForSeconds(waveRate);      // 等待生成下一波敌人
    29         }
    30     }
    31 }
  • 相关阅读:
    html基础知识
    Python yield 使用浅析
    XSS跨站攻击
    box-sizing的用法(笔记)
    编译原理中DFA最小化
    提醒自己!!!
    Eclipse下运行maven项目失败且Tomcat服务器也启动不了
    Descriptio Resource Path LocationType Archive for required library: 'D:/apache-maven/apache-maven-3.6.0/mavenrepository/org/springframework/spring-aspects/4.3.7.RELEASE/spring-aspects-4.3.7.RELEASE.
    Eclispe创建maven工程缺失web.xml报web.xml is missing and <failOnMissingWebXml> is set to true的错误
    ssm 出现 Method threw 'org.apache.ibatis.binding.BindingException' exception.Invalid bound statement (not found)……
  • 原文地址:https://www.cnblogs.com/coderJiebao/p/unity3d01.html
Copyright © 2011-2022 走看看