一些无关紧要的话:
最近心血来潮,想研究下LayaBox,因为我感觉H5是将来的趋势.而Unity对网页的支持始终不那么理想.
于是我变身小白从头开始,网上各种查资料,下载安装包,搭建环境,很顺利的就让LayaBox的案例小游戏运行起来了.
是的,就是我下面要讲的这个游戏<别让箱子掉下来>;研究了几天LayaBox,发现它很多地方其实和Unity有类似的地方,然后我突然发现
这个小游戏有点意思,决定用Unity实现一次,于是就有了这篇文章
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------
----------------------
上面的都是废话,入正题,先看游戏效果=====>
大致分析下这个游戏==>主要功能可以分为3个模块
1.箱子下落; 2.发射子弹; 3.碰撞检测; 至于其他比如击中显示粒子,不必在意,我也是胡乱做的,没有美感
好,先上代码
//箱子代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Box : MonoBehaviour
{
private GamePlay gp;
//粒子
private ParticleSystem ps;
// Start is called before the first frame update
void Start()
{
gp = GameObject.Find("Canvas").GetComponent<GamePlay>();
ps = transform.GetChild(0).GetComponent<ParticleSystem>();
ps.gameObject.SetActive(false);
}
private void OnCollisionEnter2D(Collision2D collision)
{
//检测到是底部
if (collision.gameObject.name == "DownCheck")
{
OnDownCheck();
}
//检测到是子弹
else if (collision.gameObject.name == "bullet")
{
Destroy(collision.gameObject);
OnHitBullet();
}
}
void OnDownCheck()
{
Destroy(gameObject);
gp.UpdateMiss();
}
void OnHitBullet()
{
Rigidbody2D r2d = transform.GetComponent<Rigidbody2D>();
int r = Random.Range(0, 5);
//给box一个向上的力
if (r < 2)//五分之二的几率箱子向上弹
{
r2d.AddForce(Vector2.up * 500);
}
else//击碎箱子
{
r2d.gravityScale = 0;
ps.transform.SetParent(transform.parent);
ps.gameObject.SetActive(true);
ps.Play();
transform.GetComponent<Image>().color = new Color(1, 1, 1, 0);
gp.UpdateScore();
Destroy(gameObject, 1.25f);
}
}
}
子弹代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//添加一个2d刚体
Rigidbody2D rig = gameObject.AddComponent<Rigidbody2D>();
//去掉重力
rig.gravityScale = 0;
//添加一个向上的力*系数
rig.AddForce(Vector2.up * 500);
}
private void Update()
{
//子弹超出屏幕自动销毁
if (transform.localPosition.y > Screen.height + 200)
{
Destroy(this.gameObject);
}
}
}
主逻辑
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GamePlay : MonoBehaviour
{
//文本显示
public Text Txt_score, Txt_miss, Txt_begin;
//预制体
public Box box;
public Bullet bullet;
//节点由于存放生成的子弹和箱子
public Transform bulletRoot, boxRoot;
//箱子下落时差标志
float _boxCreateFps = 0;
//得分,错过记录
int _scroe, _miss;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//鼠标左键发射
if (Input.GetMouseButtonDown(0))
{
Txt_begin.text = "";
Vector3 pos = Input.mousePosition;
pos = Camera.main.ScreenToWorldPoint(pos);
CreateBullet(pos);
}
//每隔1.25秒生成一个箱子
_boxCreateFps += Time.deltaTime;
if (_boxCreateFps >= 1.25f)
{
_boxCreateFps = 0;
//随机箱子的x坐标
float rand_x = Random.Range((Screen.width / -2) + 250, (Screen.width / 2) - 250);
Vector3 pos = new Vector3(rand_x, Screen.height + 110, 0);
CreateBox(pos);
}
}
//创建子弹
void CreateBullet(Vector3 pos)
{
GameObject go = Instantiate(bullet.gameObject) as GameObject;
go.transform.SetParent(bulletRoot);
go.transform.position = pos;
go.transform.localPosition = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0);
go.transform.localScale = Vector3.one;
go.name = "bullet";
}
//创建箱子
void CreateBox(Vector3 pos)
{
GameObject go = Instantiate(box.gameObject) as GameObject;
go.transform.SetParent(boxRoot);
go.transform.localPosition = pos;
go.transform.localPosition = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0);
float r = Random.Range(1, 2.5f);
go.transform.localScale = new Vector3(r, r, 1);
}
//更新得分
public void UpdateScore()
{
_scroe += 1;
Txt_score.text = "击中:" + _scroe;
}
//更新错过
public void UpdateMiss()
{
_miss += 1;
Txt_miss.text = "错过:" + _miss;
}
}
//场景和资源
项目包链接:
链接: https://pan.baidu.com/s/1eWIiV_ShgdMl30FwQnLIvA 提取码: 5efq
----------------------------------------------------------------------------------------------------------
需要注意:
box之间不需要碰撞,需要在设置里边剔除,不懂的可以自行百度