Demo_塔防(自动生成怪物,导航,炮塔攻击,怪物掉血死忙)
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_塔防(自动生成怪物,导航,炮塔攻击,怪物掉血死忙)的更多相关文章
- 使用Python从Markdown文档中自动生成标题导航
概述 知识与思路 代码实现 概述 Markdown 很适合于技术写作,因为技术写作并不需要花哨的排版和内容, 只要内容生动而严谨,文笔朴实而优美. 为了编写对读者更友好的文章,有必要生成文章的标题导航 ...
- PHP自动生成后台导航网址的最佳方法
'http://www.jbxue.com'=> '脚本学堂首页', </script>
- Markdown使用TOC自动生成导航栏
经常使用markdown 的玩家一定很想要一个自动生成的导航栏吧,自己写的基本思路就是 轮询监听滚动条的位置,通过抛锚和跳锚实现,这里介绍一下今天的主角,markdown-toc插件: https:/ ...
- vite插件-自动生成vue组件文档
特点 支持热更新 快速启动,依赖于 vite,无需另起服务 自动生成组件导航 ui 采用了vant-ui的样式 核心方法覆盖率达到了 92.86% 使用 yarn add vite-plugin-vu ...
- SpringBoot系列——Spring-Data-JPA(究极进化版) 自动生成单表基础增、删、改、查接口
前言 我们在之前的实现了springboot与data-jpa的增.删.改.查简单使用(请戳:SpringBoot系列——Spring-Data-JPA),并实现了升级版(请戳:SpringBoot系 ...
- fiddler4自动生成jmeter脚本
接口.性能测试任务当遇到从浏览器或移动app自己抓包的情况出现时就变得巨苦逼了,苦在哪里?苦在需要通过抓包工具抓报文,需要通过抓包报文梳理业务逻辑.需要将梳理的逻辑编写成脚本.最最苦的情况是,自己抓包 ...
- JavaScript自动生成博文目录导航
转载于:JavaScript自动生成博文目录导航 我们在写博客的时候,如果博文里面有目录,会给人结构清晰.一种一目了然的感觉,看目录就知道这篇博文要讲解的内容,并且点击目录标题就可以跳转到 具体的内容 ...
- JavaScript自动生成博文目录导航/TOP按钮
博客园页面添加返回顶部TOP按钮 进入网页管理->设置 在"页面定制CSS代码"中添加如下css样式,当然你可以改为自己喜欢的样式 此处可以将背景色background-co ...
- JavaScript:自动生成博文目录导航
感谢 孤傲苍狼 分享了 自动生成博文目录的方法,本文仅作存档使用. 图 1:效果预览 CSS 样式 #TOCbar{ font-size:12px; text-align:left; position ...
随机推荐
- Linux抓包工具tcpdump详解
tcpdump是一个用于截取网络分组,并输出分组内容的工具,简单说就是数据包抓包工具.tcpdump凭借强大的功能和灵活的截取策略,使其成为Linux系统下用于网络分析和问题排查的首选工具. tcpd ...
- JeeSite试用
JeeSite主要定位于企业信息化领域.网址:http://www.oschina.net/p/jeesite 从描述来看,各种NB,下来看的最主要原因是最近还在更新,觉得有问题可以有一批人一起研究研 ...
- PHP 用户注册
注册页面 reg.html 负责收集用户填写的注册信息.教程里只列出关键的代码片段,完整的代码附在本节最后. 注册表单 <fieldset> <legend>用户注册</ ...
- 搜索所有的路径-矩阵运算-暴力-ACM
给定一个n*n整数矩阵,定义对I行的SHIFT操作( 0 <= i < n ),是将第I行所有元素都右移一位,最右边的移到最左边. 你可以对任意行进行任意次SHIFT操作,使得: max0 ...
- utf8_general utf8_general utf8_bin区别
对与general来说 ß = s 是为true的 但是对于unicode来说 ß = ss 才是为true的, 其实他们的差别主要在德语和法语上,所以对于我们中国人来说,一般使用general,因为 ...
- ios中关于delegate(委托)的使用心得
ios中关于delegate(委托)的使用心得 分类: iOS开发2012-05-15 10:54 34793人阅读 评论(9) 收藏 举报 iosuiviewtimerinterfaceprinti ...
- 跨平台的CStdString类,实现了CString的接口
在实际工作中,std的string功能相对于MFC的CString来说,实在是相形见绌. CStdString类实现了CString的功能,支持跨平台. // ==================== ...
- C++关于构造函数的深拷贝与浅拷贝
首先拷贝构造函数的声明方法: 类名::类名(const 类名&) 1.浅拷贝的实现程序: #include "stdafx.h" #include <iostream ...
- 自动垂直居中的js
var _htmlheight; function start(){ _htmlheight=document.body.scrollHeight; resize(); /*$("#copy ...
- poj 2432 Around the world bfs+哈希
由于每个点的状态包含走过来的距离,所以要存二维的状态,但是状态总量太多,所以可以用哈希来搞. 那么就是bfs最短路,哈希记录状态了. #include <iostream> #includ ...