Tower Defence Demo

四人小组作业,通过unity实现
塔攻击的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class AttackTower : MonoBehaviour
{Transform firePosition;public GameObject bulletPrefab;// Start is called before the first frame updatevoid Start(){firePosition = transform.Find("FirePosition");}// Update is called once per framevoid Update(){}public void Fire(Transform _target){GameObject bullet = GameObject.Instantiate(bulletPrefab, firePosition.position, firePosition.rotation);bullet.GetComponent<Missile>().SetTarget(_target.gameObject.transform);Destroy(this);}
}

子弹攻击代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Bullet : MonoBehaviour
{public int damage = 50;public float speed = 20;public GameObject explosionEffectPrefab;private Transform target;// Update is called once per framevoid Update(){if (target == null){Destroy(this.gameObject);return;}transform.LookAt(target);transform.eulerAngles += new Vector3(0, -90, 90);transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * speed);}public void SetTarget(Transform _target){this.target = _target;}private void OnTriggerEnter(Collider other){if(other.tag == "Enemy"){if (other.GetComponent<Enemy1>() != null){other.GetComponent<Enemy1>().TakeDamage(damage);}else{other.GetComponent<Enemy2>().TakeDamage(damage);}GameObject.Instantiate(explosionEffectPrefab, transform.position, transform.rotation);Destroy(this.gameObject);}if(other.tag == "Shield"){Debug.Log("击中护盾");Destroy(this.gameObject);}}
}

生成敌人波次

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class EnemySpawner : MonoBehaviour
{public static int enemyAliveCount = 0;public Wave[] waves;public Transform START;public Transform enemyGroup;public Transform START2;public Transform enemyGroup2;private Coroutine coroutine;//  public static int enemyAliveCount2 = 0;public Wave[] waves2;// Start is called before the first frame updatevoid Start(){coroutine = StartCoroutine(SpawnerEnemy());}public void stop(){StopCoroutine(coroutine);}IEnumerator SpawnerEnemy(){foreach (Wave wave in waves){for(int i = 0; i < wave.count; i++){GameObject.Instantiate(wave.enemyPrefab, START.position, Quaternion.identity,enemyGroup);enemyAliveCount++;yield return new WaitForSeconds(wave.rate);//等待每个敌人出现间隔的时差}//while(enemyAliveCount != 0)//{//    yield return 0;//}yield return new WaitForSeconds(wave.waveRate);//等待每波之间的时差}foreach (Wave wave in waves2){for (int i = 0; i < wave.count; i++){GameObject.Instantiate(wave.enemyPrefab, START2.position, Quaternion.identity, enemyGroup2);enemyAliveCount++;yield return new WaitForSeconds(wave.rate);//等待每个敌人出现间隔的时差}//while(enemyAliveCount != 0)//{//    yield return 0;//}yield return new WaitForSeconds(wave.waveRate);//等待每波之间的时差}while(enemyAliveCount>0){yield return 0;}Debug.Log("aaaaa");Debug.Log(gameObject);Gamemanager.Instance.Win();}
}

敌人角色脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class Enemy1 : MonoBehaviour
{private Transform[] positions;[SerializeField]private int index = 0;[SerializeField]private float speed = 10.0f;[SerializeField] private float hp = 200;private float totalHp;private Slider hpSlider;// Start is called before the first frame updatevoid Start(){positions = SwapPoint.positions;totalHp = hp;hpSlider = GetComponentInChildren<Slider>();TurnRotation();}// Update is called once per framevoid FixedUpdate(){Move();}void Move(){transform.position = Vector3.MoveTowards(transform.position, positions[index].position, Time.deltaTime * speed);//transform.Translate((positions[index].position-transform.position).normalized *Time.deltaTime * speed);if (Vector3.Distance(positions[index].position, transform.position) < 0.4f)//到达转换点,去寻找下一个目标点,同时做出相应的旋转{index++;if (index > positions.Length - 1){ReachDestination();return;}TurnRotation();}}void TurnRotation(){Vector3 rot = transform.eulerAngles;rot.y += positions[index].transform.eulerAngles.y;transform.eulerAngles = rot;}void ReachDestination(){Gamemanager.Instance.Failed();EnemySpawner.enemyAliveCount--;Destroy(this.gameObject);}void Death(){EnemySpawner.enemyAliveCount--;}public void TakeDamage(float damage){if (hp < 0) return;hp -= damage;hpSlider.value = (float)hp / totalHp;if(hp  <= 0){BuildManager.instance.money += 10;EnemySpawner.enemyAliveCount--;Destroy(this.gameObject);}}
}

管理敌人路径

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SwapPoint : MonoBehaviour
{public static Transform[] positions;// Start is called before the first frame updateprivate void Awake(){positions = new Transform[transform.childCount];for(int i =0;i < positions.Length; i++){positions[i] = transform.GetChild(i);}}// Update is called once per framevoid Update(){}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Turret : MonoBehaviour
{public List<GameObject> enemys = new List<GameObject>();public int attackRateTime = 1;//多少秒攻击一次private float timer = 0;//计时器public GameObject bulletPrefab;//public Transform  firePosition;public bool useLaser = false;public float damagerate = 60;public LineRenderer laserrenderer;public GameObject LaserEffect;public MapCube mapcube;private void OnTriggerEnter(Collider other){if(other.tag == "Enemy"){enemys.Add(other.gameObject);if (other.GetComponent<AttackTower>() != null){other.GetComponent<AttackTower>().Fire(this.transform);}}}private void OnTriggerExit(Collider other){if (other.tag == "Enemy"){enemys.Remove(other.gameObject);}}private void Start(){firePosition = this.transform.Find("FirePosition");timer = attackRateTime;}private void Update(){if (useLaser == false){timer += Time.deltaTime;if (enemys.Count > 0 && timer >= attackRateTime){timer = 0f;Attack();}}else if(enemys.Count>0){if(laserrenderer.enabled == false)laserrenderer.enabled = true;LaserEffect.SetActive(true);if(enemys[0] == null){enemys.RemoveAll(c => c.gameObject == null);}if(enemys.Count>0){laserrenderer.SetPositions(new Vector3[] { firePosition.position, enemys[0].transform.position });if(enemys[0].GetComponent<Enemy1>() != null){enemys[0].GetComponent<Enemy1>().TakeDamage(damagerate * Time.deltaTime);}else{enemys[0].GetComponent<Enemy2>().TakeDamage(damagerate * Time.deltaTime);}LaserEffect.transform.position = enemys[0].transform.position;Vector3 pos = transform.position;pos.y = enemys[0].transform.position.y;LaserEffect.transform.LookAt(pos);}}else{LaserEffect.SetActive(false);laserrenderer.enabled = false;}}void Attack(){if (enemys[0] == null){enemys.RemoveAll(c => c.gameObject == null);}if(enemys.Count > 0){GameObject bullet = GameObject.Instantiate(bulletPrefab, firePosition.position, firePosition.rotation);bullet.GetComponent<Bullet>().SetTarget(enemys[0].transform);}}public void RebackMapCube(){mapcube.isUpgraded = false;mapcube.turretGo = null;mapcube.turretGoData = null;}
}

图形学课设 塔防游戏相关推荐

  1. 用Unity开发一款塔防游戏(一):攻击方设计

    大家好.偶尔想起了这个手把手教学的.但现已长满杂草的坑,还是来挖几铲子. 这一期的游戏是最常见的类型之一--塔防. 塔防游戏相信大家并不陌生,几个主要元素如下: 1.敌方士兵 2.我方防御塔 3.我方 ...

  2. QT 框架搭建,用最原始的方法实现简单的塔防游戏 | 原力计划

    作者 | 白家名 责编 | 王晓曼 出品 | CSDN博客 本文作者使用 QT 框架写了一个塔防游戏程序,该程序中实现了购买炮塔.炮塔升级.怪物按照设定路径移动.炮塔自动寻找范围内目标.朝目标怪物发射 ...

  3. QT实现简单的塔防游戏

    QT实现简单的塔防游戏 该程序中实现了购买炮塔.炮塔升级.怪物按照设定路径移动.炮塔自动寻找范围内目标.朝目标怪物发射炮弹.爆炸效果.怪物走到家时我方生命值减少.方便添加关卡等功能. 另附重构版本代码 ...

  4. 【Unity小游戏】游戏开发案例,轻松打造一款塔防游戏!(下)

    欢迎来到如何在 Unity 中创建塔防游戏的第二部分.你正在Unity中制作一个塔防游戏,在第一部分结束时,你可以放置和升级怪物.你还有一个敌人攻击饼干. 然而,敌人不知道该面对哪条路!此外,这是攻击 ...

  5. (译)如何使用cocos2d制作一个塔防游戏:引子

    原文链接地址:http://www.iphonegametutorials.com/2011/04/11/cocos2d-game-tutorial-how-to-build-a-tower-defe ...

  6. 塔防游戏的路径寻找算法分析

    在塔防游戏中,有很多敌人都是向着同一目标前进的.在众多塔防游戏当中,有一条或几条预定好的路径.在一些塔防游戏中,比如经典的<Desktop Tower Defense>,你可以将塔放在地图 ...

  7. cocos2d-x游戏实例(10)-塔防游戏(修改地图图素,地图整体缩放)

    小满(bill man)个人原创,欢迎转载,转载请注明地址,小满(bill man)的专栏地址http://blog.csdn.net/bill_man 塔防游戏在目前的智能机游戏中占据很重要的部分, ...

  8. unity塔防游戏怪物转向_Unity官方新手游戏项目推荐合集

    Unity官方新手游戏项目推荐合集 今天给同学们介绍一些Unity官方发布过的一些游戏项目,这些项目都简化了游戏开发的入门学习过程,可以快速地制作出游戏,适合新手入门体验,下面就带同学们看一看: Un ...

  9. 如何制作一个塔防游戏 Cocos2d x 2 0 4

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 本文实践 ...

最新文章

  1. 密码学基础知识(五)序列密码
  2. opencv进阶学习笔记14:分水岭算法 实现图像分割
  3. 鼠标滚动倾斜分割切换
  4. 网站开发与客户之间的流程
  5. 【本周面试题】第5周 - 开发工具相关
  6. XML解析(二),DOM解析XML
  7. Winfrom窗体应用程序___DataGridView
  8. 我的个人作品——室内效果图
  9. 安卓系统抓包工具大全
  10. 【BP数据预测】基于matlab狼群算法优化BP神经网络数据预测【含Matlab源码 658期】
  11. 解决打开EXCEL插件时报错“配置系统未能初始化”的问题
  12. mac怎么无线打印机连接到服务器,Mac电脑怎么连接打印机
  13. What is CRA?
  14. 博士申请 | 美国佐治亚理工学院陶默雷教授招收机器学习方向全奖博士生
  15. unison + inotify 实现文件实时双向同步部署步骤
  16. 计算机博士有哪些专业方向,国内计算机专业博士研究方向-20210619140356.docx-原创力文档...
  17. 证明三角形中cosA+cosB+cosC=1+4sin(A/2)sin(B/2)sin(C/2)
  18. ACM模版-f_zyj v 2.0——更新通知
  19. java学习php(一)基础知识
  20. DTI脑网络构建 详细介绍处理过程以及PANDA的使用

热门文章

  1. CCleaner的命令行参数教程
  2. 青龙面板快手极速版教程
  3. adb按键精灵_自动点击器adb版最新版下载|自动点击器adb版安卓版下载 v1.0.5 - 跑跑车安卓网...
  4. view_video.php,android,_Android开发,使用ViewVideo无法播放mp4文件?,android - phpStudy
  5. 2021年登高架设考试APP及登高架设试题及解析
  6. Java上机实践四实验一机动车
  7. 神经网络在科研中的应用,神经网络理论与应用
  8. NoSQL Manager for MongoDB Professional 算法
  9. 城乡投票源码php_PHP微信公众号投票活动系统源码 独立版
  10. 使用go实现反向代理