using UnityEngine;
using System.Collections; public struct WaveMsg
{
//该波次生成的怪物
public GameObject monster;
//时间间隔
public float waveInterval;
//生成个数
public float count;
//生成间隔
public float interval;
//怪物血量
public float hp; /// <summary>
/// 构造函数
/// </summary>
/// <param name="monster">Monster.</param>
/// <param name="waveInterval">Wave interval.</param>
/// <param name="count">Count.</param>
/// <param name="interval">Interval.</param>
public WaveMsg(GameObject monster,float waveInterval,
float count,float interval,float hp)
{
this.monster = monster;
this.waveInterval = waveInterval;
this.count = count;
this.interval = interval;
this.hp = hp;
}
} public class MonsterInit : MonoBehaviour { //怪物预设体
public GameObject[] monsters;
//波次信息
public WaveMsg[] waveMsg;
//每波怪之间的时间计时器
private float waveTimer;
//每个怪之前的时间计时器
private float monsterTimer;
//波次计数器
private int count;
//怪物个数计数器
private int monsterCount;
//游戏开关
private bool gameBegin = true; void Awake()
{
waveMsg = new WaveMsg[]{
new WaveMsg(monsters[],,,,),
new WaveMsg(monsters[],2.5f,,1.5f,),
new WaveMsg(monsters[],,,1f,),
new WaveMsg(monsters[],,,0.5f,),
new WaveMsg(monsters[],,,0.5f,)
};
} void Update()
{
if (gameBegin) {
//波次计时器计时
waveTimer += Time.deltaTime;
if (waveTimer >= waveMsg [count].waveInterval) {
//如果当前生成怪的个数不够,继续生成
if (monsterCount < waveMsg [count].count) {
//计时器计时
monsterTimer += Time.deltaTime;
//计时结束
if (monsterTimer > waveMsg [count].interval) {
//生成怪物
GameObject currentMaster = Instantiate (
waveMsg [count].monster,
transform.position,
Quaternion.identity) as GameObject;
//给新生成的怪物设置血量值
currentMaster.GetComponent<Monster> ().hp =
waveMsg [count].hp;
//怪物个数++
monsterCount++;
//计时器清零
monsterTimer = ;
}
} else {
//波次加一
count++;
if (count < waveMsg.Length) {
//怪物个数清零
monsterCount = ;
//波次计时器清零
waveTimer = ;
} else {
//表示怪兽生成结束
gameBegin = false;
}
}
}
}
}
}

using UnityEngine;
using System.Collections; public class GameOver : MonoBehaviour { //血量
public int hp = ; //触发
void OnTriggerEnter(Collider other)
{
//如果是怪物
if (other.tag == "Monster") {
//减血操作
if (--hp <= ) {
Debug.Log ("GameOver");
}
//销毁怪物
Destroy (other.gameObject);
}
}
}

using UnityEngine;
using System.Collections;
using System.Collections.Generic; public class GaussAttack : MonoBehaviour { //攻击队列
Queue<Transform> monsters;
//当前攻击目标
public Transform currentTarget;
//炮筒旋转速度
public float turnSpeed = ;
//发射特效
private GameObject muzzle;
//炮塔伤害值
public float damage = ; void Awake()
{
monsters = new Queue<Transform> ();
muzzle = transform.Find ("Base/Turret/Muzzle_1").gameObject;
} void Update()
{
if (currentTarget) {
//转向
LookAtTargetSmooth();
//给敌人减血
currentTarget.GetComponent<Monster>().hp-=Time.deltaTime * damage;
//如果当前目标已经死亡
if (!currentTarget.GetComponent<Monster> ().monsterAlive) {
//切换下一个目标
TargetSwitch ();
}
}
} /// <summary>
/// 平滑旋转看向敌人
/// </summary>
void LookAtTargetSmooth()
{
//方向向量
Vector3 dir = currentTarget.position - transform.position;
//目标旋转
Quaternion qua = Quaternion.LookRotation (dir);
//先找到炮筒
Transform gun = transform.GetChild ().GetChild ();
//炮筒转向目标四元数
gun.rotation = Quaternion.Lerp (gun.rotation, qua,
Time.deltaTime * turnSpeed);
} /// <summary>
/// 进入攻击范围
/// </summary>
/// <param name="other">Other.</param>
void OnTriggerEnter(Collider other)
{
//如果是怪物
if (other.tag == "Monster") {
//进队列
monsters.Enqueue (other.transform);
// 如果没有攻击目标
if (currentTarget == null) {
TargetSwitch ();
}
}
} void OnTriggerExit(Collider other)
{
//如果是怪物,且在队列内
if (other.tag == "Monster" && other.transform == currentTarget) {
//切换下一个目标
TargetSwitch ();
}
} /// <summary>
/// 切换目标
/// </summary>
void TargetSwitch()
{
//如果队列中有数据
if (monsters.Count > ) {
//队列中出来的怪物即为下一个攻击目标
currentTarget = monsters.Dequeue ();
//有怪,开枪
muzzle.SetActive (true);
} else {
//如果队列中没有数据,此时目标为空
currentTarget = null;
//没怪,停止开枪
muzzle.SetActive (false);
}
}
}

using UnityEngine;
using System.Collections; public class CameraMove : MonoBehaviour { private float hor,ver;
public float moveSpeed = ; void Update()
{
hor = Input.GetAxis ("Horizontal");
ver = Input.GetAxis ("Vertical"); transform.position += new Vector3 (hor,,ver)
* Time.deltaTime * moveSpeed;
//24 0 2
//40 0 35 transform.position = new Vector3 (
Mathf.Clamp(transform.position.x,24f,40f),
transform.position.y,
Mathf.Clamp(transform.position.z,2f,35f)
); } }
using UnityEngine;
using System.Collections; public class TowerInit : MonoBehaviour { //射线碰撞检测器
RaycastHit hit;
//炮塔预设体
public GameObject towerPrefab; void Update()
{
//获取屏幕射线
Ray r = Camera.main.ScreenPointToRay (
Input.mousePosition);
//射线碰撞到了碰撞体
if (Physics.Raycast (r, out hit)) {
//如果该物体名字中包含‘Base’字符串,且基座没有子物体
if (hit.collider.name.Contains ("Base") &&
hit.transform.childCount == ) {
if (Input.GetMouseButtonDown ()) {
///TODO:生成炮塔
GameObject currentTower = Instantiate (
towerPrefab,
hit.transform.position + Vector3.up * 2.7f,
Quaternion.identity) as GameObject;
//设置炮塔为基座的子物体
currentTower.transform.SetParent (hit.transform);
}
}
}
} }

炮塔需要触发事件设置好范围来检测怪物,进入范围触发事件攻击。

地图设置导航。

怪物添加刚体,设置动力学。避免出现碰撞弹奏,或者穿透现象。

Demo_塔防(自动生成怪物,导航,炮塔攻击,怪物掉血死忙)的更多相关文章

  1. 使用Python从Markdown文档中自动生成标题导航

    概述 知识与思路 代码实现 概述 Markdown 很适合于技术写作,因为技术写作并不需要花哨的排版和内容, 只要内容生动而严谨,文笔朴实而优美. 为了编写对读者更友好的文章,有必要生成文章的标题导航 ...

  2. PHP自动生成后台导航网址的最佳方法

    'http://www.jbxue.com'=> '脚本学堂首页', </script>

  3. Markdown使用TOC自动生成导航栏

    经常使用markdown 的玩家一定很想要一个自动生成的导航栏吧,自己写的基本思路就是 轮询监听滚动条的位置,通过抛锚和跳锚实现,这里介绍一下今天的主角,markdown-toc插件: https:/ ...

  4. vite插件-自动生成vue组件文档

    特点 支持热更新 快速启动,依赖于 vite,无需另起服务 自动生成组件导航 ui 采用了vant-ui的样式 核心方法覆盖率达到了 92.86% 使用 yarn add vite-plugin-vu ...

  5. SpringBoot系列——Spring-Data-JPA(究极进化版) 自动生成单表基础增、删、改、查接口

    前言 我们在之前的实现了springboot与data-jpa的增.删.改.查简单使用(请戳:SpringBoot系列——Spring-Data-JPA),并实现了升级版(请戳:SpringBoot系 ...

  6. fiddler4自动生成jmeter脚本

    接口.性能测试任务当遇到从浏览器或移动app自己抓包的情况出现时就变得巨苦逼了,苦在哪里?苦在需要通过抓包工具抓报文,需要通过抓包报文梳理业务逻辑.需要将梳理的逻辑编写成脚本.最最苦的情况是,自己抓包 ...

  7. JavaScript自动生成博文目录导航

    转载于:JavaScript自动生成博文目录导航 我们在写博客的时候,如果博文里面有目录,会给人结构清晰.一种一目了然的感觉,看目录就知道这篇博文要讲解的内容,并且点击目录标题就可以跳转到 具体的内容 ...

  8. JavaScript自动生成博文目录导航/TOP按钮

    博客园页面添加返回顶部TOP按钮 进入网页管理->设置 在"页面定制CSS代码"中添加如下css样式,当然你可以改为自己喜欢的样式 此处可以将背景色background-co ...

  9. JavaScript:自动生成博文目录导航

    感谢 孤傲苍狼 分享了 自动生成博文目录的方法,本文仅作存档使用. 图 1:效果预览 CSS 样式 #TOCbar{ font-size:12px; text-align:left; position ...

随机推荐

  1. 在CentOS 6.3中安装与配置JDK-7

    在CentOS 6.3中安装与配置JDK-7 来源:互联网 作者:佚名 时间:02-07 16:28:33 [大 中 小] 在CentOS-6.3中安装与配置JDK-7,有需要的朋友可以参考下 安装说 ...

  2. prototype/constructor/__proto__之prototype简单应用

    一.简单使用构造原型加prototype造简单的轮子. 1.想jQ那样获取HTML元素,先看JS代码 function Cmf() { //创建构造函数 this.arry = [] } Cmf.pr ...

  3. php 上传视频的代码

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  4. Linux系统分区

    在Linux系统里面,"分区",被称作"挂载点" 挂载点 意思就是把一部分硬盘容量,分成一个文件夹的形式,用来做某些事情,这个文件夹的名字,就叫做:挂载点 (如 ...

  5. python学习笔记(一)元组,序列,字典

    python学习笔记(一)元组,序列,字典

  6. gcc链接g++编译生成的静态库和动态库的makefile示例

    使用c++开发程序或者库时,将库提供给其他人使用. 然而使用者是使用c开发的程序,链接g++编译生成的库时,于链接gcc生成的库,有所不同. 首先是静态库,以链接g++编译生成的libmylib.a为 ...

  7. 启明星辰:安全管理平台(SOC)

    泰 合信息安全运营中心(Security Operation Center)系统是一个以IT资产为基础,以业务信息系统为核心,以客户体验为指引,从监控.审计.风险.运维四个维度建立起来的一套可度量的统 ...

  8. java学习面向对象之继承

    在我们编写程序的过程当中,会遇到这种情况: 比如现在有一个狗,他的功能有跑,有跳,有吃,有叫,属性有雌雄,大小,颜色等等,同时现在我们也有一个猫,上述功能她也有.这个时候我们写代码的时候,就得分别把上 ...

  9. java学习面向对象之设计模式之单例模式

    就像上一节当中我们讲到的数组工具集一样,如果我们把他看作一个类,来应用,不阻止他new函数的话,这个类我们在整个过程当中我们只是用他来当一个工具.假如每次用都要new一下产生一个新对象的话,就会显得整 ...

  10. C++中强制变换之const_cast

    今天学习了一下C++中的强制转换,看了const_cast,我发现了这个转换关键字的奇怪之处,于是把它记录一下,废话不说,先看一个程序: #include <iostream> using ...