您的位置:首页 > 移动开发 > Unity3D

【个人Unity笔记】{基础} 2D小游戏一个简单的自动刷怪脚本

2016-10-28 14:19 781 查看
效果如图,小游戏的话效果还可以



脚本使用DOTween做了顿帧

在场景中新建一个空物体GameController

创建一个C#脚本GameController.cs

把下面的复制进去

using UnityEngine;
using System.Collections;
using DG.Tweening;

public class GameController :MonoBehaviour
{
[Header("第一种敌人")]
public GameObject type01;// 存放敌人prefab
public GameObject type01Death;// 存放敌人死亡prefab
public int type01Count;// type01每一波敌人的个数
public float type01spawnTime;// 每一波中生成下一个敌人的时间间隔
public int type01MaxNum;//场上最大怪物数量
public int type01EnemyNum;//场上的怪物数量
[Header("第二种敌人")]
public GameObject type02;
public GameObject type02Missile;
public GameObject type02Death;// 存放敌人死亡prefab
public int type02ShowScore;//type02多少分以后才会生成
public int type02Count;// type02每一波敌人的个数
public float type02spawnTime;// 每一波中生成下一个敌人的时间间隔
public int type02MaxNum;//场上最大怪物数量
public int type02EnemyNum;//场上的怪物数量
[Header("第三种敌人")]
public GameObject type03;
public GameObject type03Death;// 存放敌人死亡prefab
public int type03ShowScore;//type03多少分以后才会生成
public int type03Count;
public float type03spawnTime;// 每一波中生成下一个敌人的时间间隔
public int type03MaxNum;//场上最大怪物数量
public int type03EnemyNum;//场上的怪物数量
[Header("开始游戏后玩家的准备时间")]
public float startWait;// 开始游戏后玩家的准备时间
[Header("生成下一波敌人的等待时间")]
public float waveWait;// 生成下一波敌人的等待时间
[Header("场上最大怪物总数量")]
public int MaxEnemyNum;//场上最大怪物数量
[System.NonSerialized]
public float HitEndTime;//打击感
[System.NonSerialized]
public int ScoreLate=1;
//[System.NonSerialized]
public int Score;//分数
//[System.NonSerialized]
public int EnemyNum;//场上的怪物数量

void Start()
{
StartCoroutine(spawnWaves());

}
void Update()
{
//击中敌人顿帧
if(HitEndTime < Time.realtimeSinceStartup)
Time.timeScale = 1f;
if(Score>= ScoreLate)
{
ScoreLate = Score + 1;
DOTween.Goto("MainCamShake",0f,true);
Time.timeScale = 0.5f;
HitEndTime = Time.realtimeSinceStartup + 0.1f;
}
}
// 协同函数
IEnumerator spawnWaves()
{
// 开始游戏后,不会立即有敌人,需要给玩家一些准备时间waitTime
yield return new WaitForSeconds(startWait);
// 循环生成一波一波的敌人
while(EnemyNum<= MaxEnemyNum)
{
Quaternion spawnRotation = Quaternion.identity;
//生成type01
for(int i = 0 ; i < type01Count ; ++i)
{
if(type01EnemyNum< type01MaxNum)
{
Vector3 spawnPosition = new Vector3(Random.Range(-8f, 8f), -3f, 0f);//这里刷怪位置可以修改成你要的位置
Instantiate(type01, spawnPosition, spawnRotation);
type01EnemyNum++;
EnemyNum++;
}
// 每个敌人生成有时间间隔
yield return new WaitForSeconds(type01spawnTime);
}
//生成type02
for(int i = 0 ; i < type02Count ; ++i)
{
if(Score >= type02ShowScore & type02EnemyNum < type02MaxNum)
{
float[] enemy0002spawn = { -7f, 7f, 7f };//enemy0002刷新位置X坐标
Instantiate(type02, new Vector3(enemy0002spawn[Random.Range(0, 2)], -3f, 0f), spawnRotation);
type02EnemyNum++;
EnemyNum++;
}
// 每个敌人生成有时间间隔
yield return new WaitForSeconds(type02spawnTime);
}
//生成type03
for(int i = 0 ; i < type03Count ; ++i)
{
//加入type03
}
yield return new WaitForSeconds(waveWait);
}
}
}


分数的加减是写在怪物自己的脚本里面的

这是type01怪物死亡时,加一分,场上怪物减一,同种类怪物减一

void OnTriggerEnter(Collider coll)
{
if(coll.gameObject.name == "Weapon")
{
GameControll.GetComponent<GameController>().Score += 1;
GameControll.GetComponent<GameController>().type01EnemyNum -= 1;
GameControll.GetComponent<GameController>().EnemyNum -= 1;
Instantiate(GameControll.GetComponent<GameController>().type01Death, gameObject.transform.position, gameObject.transform.localRotation);
Destroy(gameObject);
}
}


此脚本只负责刷怪,脚本里面public GameObject很多,在其他脚本中直接GameObject.Find就可以获取到,比如怪物的死亡,死亡动画我是另外做了一个Prefab,死亡的Prefab自动播放死亡动画,播完销毁,所以怪物死亡的时候在原地实例化死亡的Prefab,随后销毁自己。这个死亡的Prefab我就放在这个脚本里,其他脚本直接就能获取到,因为GameController始终存在与场景中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity 游戏 c# 脚本
相关文章推荐