做一个简单的塔防游戏

1、设置一个Create Empty 挂上三个脚本取名GameControlScript、CreatTowerScript、CreatEnemyScripts

(1)、GameControlScript

sing System.Collections;
using System.Collections.Generic;
//这个脚本
//1、控制怪物的产生
//2、保存怪物的相关信息
public class GameControlScript : MonoBehaviour {
//将一波怪物的相关信息保存在结构体当中
public struct Enemy
{
//1、每一个的血量
public int Enemy_Hp;
//2、每一个怪物的速度
public int Enemy_Speed;
//3、每一波怪物刷新的时间
public float Level_RefreshTime;
//4、每一波怪的个数
public int Level_Count;
}
public static GameControlScript instance;
void Awake()
{
instance = this;
}


public Enemy[] enemy ;
//随机产生波数
public int Levels ;
//第一波怪物的相关属性
public int First_Hp = 1000;
public int First_Speed = 1;
public float First_RefreshTime = 3;
public int First_Count = 5;
void Start () {
Levels = Random.Range(5,8);
//根据 波数 去声明 结构体数组中 结构体的个数
enemy = new Enemy[Levels];
for (int i = 0; i < enemy.Length; i++) {
enemy [i].Enemy_Hp = First_Hp;
First_Hp += 1200;
enemy [i].Enemy_Speed = First_Speed;
First_Speed += 2;
enemy [i].Level_RefreshTime = First_RefreshTime;
First_RefreshTime += 0.5f;
enemy [i].Level_Count = First_Count;
First_Count += 2;


}
}

}

(2)、CreatEnemyScripts

//这个脚本用来控制怪物的产生

using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public class CreatEnemyScripts : MonoBehaviour {
public GameObject EnemyPrefab;
public Transform BirthPosition;
public Text text;
// Use this for initialization
void Start () {
}
//判断游戏是否结束
bool GameIsOver = false;
//判断一波怪的刷新时间是否结束
bool refreTime = true;
// Update is called once per frame
void Update () {
CreatEnemy ();


}
// 当前怪物的数量
int Current_Level_Count =0;
//波数
public int Levels = 0;
//控制多少时间产生怪物
float timerEnemy = 0;
//控制刷新间隔
float timerreftime = 0;


void CreatEnemy(){
//如果没有结束游戏结束.怪物开始刷新
if (GameIsOver == false) {
timerreftime += Time.deltaTime;
//如果怪物的刷新间隔大于GameControlScript里面的每一波怪物刷新的时间并仍然在刷新
if (timerreftime >= GameControlScript.instance.enemy [Levels].Level_RefreshTime && refreTime == true) {
//判断刷新结束
refreTime = false;
//刷新时间归零
timerreftime = 0;
}
//如果怪物的刷新间隔结束,开始生产怪物
else if (refreTime == false) {
timerEnemy += Time.deltaTime;
//每间隔1秒产生一个物体
if (timerEnemy > 1) {
GameObject enemy = Instantiate (EnemyPrefab, BirthPosition.position, Quaternion.identity) as GameObject;
enemy.GetComponent<EnemyScript> ().Hp = GameControlScript.instance.enemy [Levels].Enemy_Hp;
//当前怪物的数量+1
Current_Level_Count++;
//时间归零
timerEnemy = 0;
//如果当前产生怪物的数量等于GameControlScript里面每一波怪的个数
if (Current_Level_Count == GameControlScript.instance.enemy [Levels].Level_Count) {
//怪物开始刷新
refreTime = true;
//波数+1
Levels++;
//刷新时间归零
timerreftime = 0;
//当前怪物数量归零
Current_Level_Count = 0;
//如果 波数等于GameControlScript里面怪物数组的长度
if (Levels == GameControlScript.instance.enemy.Length) {
//游戏结束
GameIsOver = true;
Debug.Log ("这是最后一波怪了");
}
}
}
}
}
}
}

(3)、CreatTowerScript

//这个脚本用来控制炮塔的产生

using UnityEngine;
using System.Collections;


public class CreatTowerScript : MonoBehaviour {
public GameObject GunPrefab;  // 炮的预制体
// Use this for initialization
void Start () {


}

// Update is called once per frame
void Update () {
//如果点击鼠标右键,产生一条射线
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit[] infohits;
//将射线所穿过的所有物体加入这个数组内
infohits = Physics.RaycastAll (ray);
//如果射线内存在物体
if (infohits.Length > 0) {
//遍历射线
foreach (var item in infohits) {
//射线穿过的物体的tag值为tower,并且他的子物体没有任何东西
if (item.transform.tag == "tower" && item.transform.childCount == 0) {
//产生的地方为此物体的地点并且往上偏移2.7f
Vector3 GunPosition = item.transform.position + Vector3.up * 2.7f;
//创建预制体
GameObject gun = Instantiate (GunPrefab, GunPosition, Quaternion.identity) as GameObject;
//将预制体的父体设置为此物体
gun.transform.SetParent (item.transform);
}
}
}
}
}
}

2、在怪物身上挂一个脚本叫 EnemyScript

using UnityEngine;
using System.Collections;


public class EnemyScript : MonoBehaviour {
NavMeshAgent agent;
GameObject TargetPosition;
Animation myanimator;
public int Hp = 0;
//一出生就自动寻路到目标点
void Start () {
agent = GetComponent<NavMeshAgent> ();
TargetPosition = GameObject.Find ("DeadPosition");
agent.destination = TargetPosition.transform.position;
myanimator = GetComponent<Animation> ();
}

// Update is called once per frame
void Update () {

//如果距离目标点还剩0.1,就销毁物体
if (agent.remainingDistance <= 0.1f) {
Destroy (gameObject);
} else if (Hp <= 0) {
agent.Stop ();
myanimator.CrossFade ("Dead");
Destroy (gameObject, 1);
}

}
}

3、在炮的上半部分挂上一个脚本 Controlgunscript

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class Controlgunscript : MonoBehaviour {
Queue<Transform> Target; //设置攻击目标
Transform AttackTarget; //攻击物体
public GameObject bulletPrefab;
public Transform BulletPosition;
void Start () {
Target = new Queue<Transform> ();
}
 
public float angel;
//设置多少时间产生子弹
public float TimeIntval;
float fireTimer = 0;
void Update () {
//如果攻击目标为空
if (AttackTarget == null) {
//如果集合内存在物体
if (Target.Count > 0) {
//攻击目标指定为其中之一
AttackTarget = Target.Dequeue ();
}
} else{
Vector3 direct = AttackTarget.position - transform.position;
//将方向转化为旋转
Quaternion qua = Quaternion.LookRotation (direct);
每次 让 炮往 目标点的位置旋转 总 角度的 angel *Time.deltaTime
Quaternion result = Quaternion.Lerp (transform.rotation, qua, angel * Time.deltaTime);
//炮的旋转方向
transform.rotation = result;
//炮的正方向 与 敌人到炮的方向 的夹角小于10度,就开炮
if (Vector3.Angle (transform.forward, direct) < 10) {
fireTimer += Time.deltaTime;
if (fireTimer > TimeIntval) {
GameObject bullet = Instantiate (bulletPrefab, BulletPosition.position, Quaternion.identity) as GameObject;
fireTimer = 0;
//创建子弹的时候 给子弹设置攻击目标
bullet.GetComponent<fireScript>().target = AttackTarget;
}
}
}
}
//碰触检测
void OnTriggerEnter(Collider other)
{
//如果被碰触的tag值为enemy时
if (other.transform.tag == "enemy") {
//将物体加入进集合中
Target.Enqueue (other.gameObject.transform);
}
}

void OnTriggerExit(Collider other)
{
//将离开碰撞检测的物体离开集合内
if (Target.Count > 0) {
Target.Dequeue ();
}
//如果离开的物体就是攻击目标
if (other.transform == AttackTarget) {
//攻击目标为空
AttackTarget = null;
}
}
}

4、在子弹上面建立一个脚本fireScript



public class fireScript : MonoBehaviour {
//设置攻击目标
public Transform target;
//子弹的伤害值
public int damage = 100;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if (target == null) {

//如果目标为空
Destroy (gameObject);
} else {

Vector3 dir = Vector3.MoveTowards (transform.position, target.position, 0.2f);
transform.position = dir;
}
}


void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "enemy") {
other.transform.GetComponent<EnemyScript> ().Hp -= damage;
}
Destroy (gameObject);
}
}

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值