目录
1.空气墙的制作,标签的添加
2.玩家无敌方法与死亡方法的编写
3.区别玩家的子弹与敌人的子弹
4.修复bug,制作敌人的子弹
5.敌人的制作,玩家产生效果的添加
6.用同样的产生特效随机生成两种敌人
7.敌人AI的编写
8.初始化地图工具的创建
9. 编写产生随机位置的方法
10.初始化地图其他的游戏物体
在Bullet添加完善碰撞检测
private void OnTriggerEnter2D(Collider2D collision)
{
switch (collision.tag)
{
case "Tank":
break;
case "Heart":
break;
case "Enemy":
break;
case "Wall":
break;
case "Barriar":
break;
default:
break;
}
private bool isDefended = true;
private float defendTimeVal = 3;
public GameObject explosionPrefab;
public GameObject defendEffectPrefab;
//坦克的死亡方法
private void Die()
{
if (isDefended)
{
return;
}
//产生爆炸特效
Instantiate(explosionPrefab, transform.position, transform.rotation);
//死亡
Destroy(gameObject);
}
void Update () {
//是否处于无敌状态
if (isDefended)
{
defendEffectPrefab.SetActive(true);
defendTimeVal -= Time.deltaTime;
if(defendTimeVal <= 0)
{
isDefended = false;
defendEffectPrefab.SetActive(false);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour {
public float moveSpeed = 10;
public bool isPlayerBullet;
// Update is called once per frame
void Update () {
transform.Translate(transform.up * moveSpeed * Time.deltaTime, Space.World);
}
private void OnTriggerEnter2D(Collider2D collision)
{
switch (collision.tag)
{
case "Tank":
if (!isPlayerBullet)
{
collision.SendMessage("Die");
}
break;
case "Heart":
collision.SendMessage("Die");
break;
case "Enemy":
break;
case "Wall":
Destroy(collision.gameObject);
Destroy(gameObject);
break;
case "Barriar":
Destroy(gameObject);
break;
default:
break;
}
}
}
为坦克生成子弹的时候添加一条语句
GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.Euler(transform.eulerAngles + bulletEulerAngles));
bullet.GetComponent().isPlayerBullet = true;
添加敌人Enemy的脚本,新增Born脚本用于玩家的初始生成
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Born : MonoBehaviour {
public GameObject playerPrefab;
// Use this for initialization
void Start () {
Invoke("BornTank", 1f);
Destroy(gameObject, 1f);
}
// Update is called once per frame
void Update () {
}
private void BornTank()
{
Instantiate(playerPrefab, transform.position, Quaternion.identity);
}
}
在Born里添加代码
public GameObject[] enemyPrefabList;
public bool createPlayer;
private void BornTank()
{
if (createPlayer)
{
Instantiate(playerPrefab, transform.position, Quaternion.identity);
}
else
{
int num = Random.Range(0, 2);
Instantiate(enemyPrefabList[num], transform.position, Quaternion.identity);
}
}
编写敌人自动攻击和移动的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
//属性值
public float moveSpeed = 3;
private Vector3 bulletEulerAngles;
private float v;
private float h;
//引用
public Sprite[] tankSprites;//上 右 下 左
public GameObject bulletPrefab;
private SpriteRenderer sr;
public GameObject explosionPrefab;
//计时器
private float time = 3f;
private float timer = 0;
private float timeValChangeDirection;
private void Awake()
{
sr = GetComponent();
}
// Update is called once per frame
void Update()
{
//攻击的时间间隔
if (timer >= time)
{
Attack();
}
else
{
timer += Time.deltaTime;
}
}
private void FixedUpdate()
{
Move();
}
//坦克的攻击方法
private void Attack()
{
//子弹产生的角度:当前坦克的角度+子弹应该旋转的角度
if (sr.sprite == tankSprites[2])
{
bulletEulerAngles = new Vector3(0, 0, -180);
}
else if (sr.sprite == tankSprites[3])
{
bulletEulerAngles = new Vector3(0, 0, 90);
}
else if (sr.sprite == tankSprites[1])
{
bulletEulerAngles = new Vector3(0, 0, -90);
}
else
{
bulletEulerAngles = new Vector3(0, 0, 0);
}
GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.Euler(transform.eulerAngles + bulletEulerAngles));
bullet.GetComponent().isPlayerBullet = true;
timer = 0;
}
//坦克的移动方法
private void Move()
{
if(timeValChangeDirection >= 4)
{
int num = Random.Range(0, 8);
if(num > 5)
{
v = -1;
h = 0;
}
else if(num == 0)
{
v = 1;
h = 0;
}else if (num > 0 && num <= 2)
{
h = -1;
v = 0;
}else if(num >2&& num <= 4)
{
h = 1;
v = 0;
}
timeValChangeDirection = 0;
}
else
{
timeValChangeDirection += Time.fixedDeltaTime;
}
transform.Translate(Vector3.up * v * moveSpeed * Time.fixedDeltaTime, Space.World);
if (v < 0)
{
sr.sprite = tankSprites[2];
//bulletEulerAngles = new Vector3(0, 0, -180);
}
else if (v > 0)
{
sr.sprite = tankSprites[0];
}
if (v != 0)
{
return;
}
transform.Translate(Vector3.right * h * moveSpeed * Time.fixedDeltaTime, Space.World);
if (h < 0)
{
sr.sprite = tankSprites[3];
//bulletEulerAngles = new Vector3(0, 0, 90);
}
else if (h > 0)
{
sr.sprite = tankSprites[1];
//bulletEulerAngles = new Vector3(0, 0, -90);
}
}
//坦克的死亡方法
private void Die()
{
//产生爆炸特效
Instantiate(explosionPrefab, transform.position, transform.rotation);
//死亡
Destroy(gameObject);
}
}
创建MapCreation,添加创建老家的方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapCreation : MonoBehaviour {
//用来装饰初始化地图所需物体的数组
//0.老家 1.墙 2.障碍 3.出生效果 4.河流 5.草 6.空气墙
public GameObject[] item;
private void Awake()
{
//实例化老家
CreateItem(item[0], new Vector3(0, -8, 0), Quaternion.identity);
//用墙把老家围起来
CreateItem(item[1], new Vector3(-1, -8, 0), Quaternion.identity);
CreateItem(item[1], new Vector3(1, -8, 0), Quaternion.identity);
for(int i = -1; i < 2; i++)
{
CreateItem(item[1], new Vector3(i, -7, 0), Quaternion.identity);
}
}
private void CreateItem(GameObject createGameObject,Vector3 createPosition,Quaternion createRotation)
{
GameObject itemGo = Instantiate(createGameObject, createPosition, createRotation);
itemGo.transform.SetParent(gameObject.transform);
}
}
运行结果如下:
添加随机生成位置的方法,并且生成空气墙
//产生随机位置的方法
private Vector3 CreateRandomPosition()
{
//不生产x=-10,10的两列,y=-8,8这两行的位置
while (true)
{
Vector3 createPosition = new Vector3(Random.Range(-9, 10), Random.Range(-7, 8), 0);
if (!HasThePosition(createPosition))
{
return createPosition;
}
}
}
//用来判断位置列表中是否有这个位置
private bool HasThePosition(Vector3 createPos)
{
for (int i = 0; i < itemPositionList.Count; i++)
{
if(createPos == itemPositionList[i])
{
return true;
}
}
return false;
}
private void Awake()
{
//实例化外围墙
for(int i = -11; i < 12; i++)
{
CreateItem(item[6], new Vector3(i, 9, 0), Quaternion.identity);
}
for (int i = -11; i < 12; i++)
{
CreateItem(item[6], new Vector3(i, -9, 0), Quaternion.identity);
}
for (int i = -8; i < 9; i++)
{
CreateItem(item[6], new Vector3(-11, i, 0), Quaternion.identity);
}
for (int i = -8; i < 9; i++)
{
CreateItem(item[6], new Vector3(11, i, 0), Quaternion.identity);
}
}
完善初始化地图
//产生敌人的方法
private void CreateEnemy()
{
int num = Random.Range(0, 3);
Vector3 EnemyPos = new Vector3();
if(num == 0)
{
EnemyPos = new Vector3(-10, 8, 0);
}
else if (num == 1)
{
EnemyPos = new Vector3(0, 8, 0);
}
else
{
EnemyPos = new Vector3(10, 8, 0);
}
CreateItem(item[3], EnemyPos, Quaternion.identity);
}
//初始化玩家
GameObject go = Instantiate(item[3], new Vector3(-2, -8, 0), Quaternion.identity);
go.GetComponent().createPlayer = true;
//产生敌人
CreateItem(item[3], new Vector3(-10, 8, 0), Quaternion.identity);
CreateItem(item[3], new Vector3(0, 8, 0), Quaternion.identity);
CreateItem(item[3], new Vector3(10, 8, 0), Quaternion.identity);
InvokeRepeating("CreateEnemy", 4, 5);
//实例化地图
for (int i = 0; i < 60; i++)
{
CreateItem(item[1], CreateRandomPosition(), Quaternion.identity);
}
for (int i = 0; i < 20; i++)
{
CreateItem(item[2], CreateRandomPosition(), Quaternion.identity);
}
for (int i = 0; i < 20; i++)
{
CreateItem(item[4], CreateRandomPosition(), Quaternion.identity);
}
for (int i = 0; i < 20; i++)
{
CreateItem(item[5], CreateRandomPosition(), Quaternion.identity);
}
运行结果如下: