这个是完成的效果图主要实现从相机发射子弹打生成的墙体,打中后销毁
- 因为游戏比较简单直接来贴源码吧
- 这个是砖块的生成
-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CubeManger : MonoBehaviour {
///砖块
private GameObject cube;
void Start () {
///加载预设
cube = Resources.Load<GameObject>("Cube");
///生成砖块
CreatOneWall(5, 8);
}///生成一块砖块
private void CreatOneCube(Vector3 v3)
{
GameObject go = Instantiate(cube,v3,Quaternion.identity);
//给每个砖块一个颜色
go.GetComponent<MeshRenderer>().material.color =
new Color(Random.Range(0, 1f), Random.Range(0, 1f), Random.Range(0, 1f));
//这里随机数一定要记得加f或者将范围控制到小数
//给生成的砖块一个父物体
go.transform.SetParent(transform);
}
/// <summary>
/// 生成一堵墙
/// </summary>
/// <param name="H">行</param>
/// <param name="L">列</param>
private void CreatOneWall(int H,int L)
{
for (int i = 0; i < H; i++)
{
for (int j = 0; j < L; j++)
{
//生成砖块并且赋予位置
CreatOneCube(new Vector3(j-4f, i + 0.5f, 0));
}
}
}
} - 子弹生成
-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class BulletManger : MonoBehaviour {
//子弹
private GameObject bullet;void Start () {
//将子弹加载进来
bullet = Resources.Load<GameObject>("Bullet");
}
//生成一颗子弹
private void CreatOneBullet()
{
GameObject go = Instantiate(bullet, transform.position, Quaternion.identity);
go.GetComponent<MeshRenderer>().material.color =
new Color(Random.Range(0, 1f), Random.Range(0, 1f), Random.Range(0, 1f));
//将主相机设置为生成子弹的父物体
go.transform.SetParent(transform);
}
void Update () {
//点击生成子弹并且发射
if (Input.GetMouseButtonDown(0))
{
CreatOneBullet();
}
}
} - 子弹发射
-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class BulletShort : MonoBehaviour {
[Range(0,100)]
public float speed;
void Start () {
//定义子弹的声明周期
Destroy(gameObject, 3);
}
void Update () {
//生成子弹并且发射
transform.Translate(Vector3.forward * Time.deltaTime*speed);
}
private void OnCollisionEnter(Collision collision)
{
//判断标签防止将地面销毁
if (collision.gameObject.tag == "Cube")
{
Destroy(gameObject);
Destroy(collision.gameObject);
}
}
} - 相机的移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
float H = Input.GetAxis("Horizontal");
float V = Input.GetAxis("Vertical");
transform.Translate(new Vector3(H, V, 0));
}
}
- 最后留一下项目的下载地址链接:https://pan.baidu.com/s/1qWhBuYWVqYRzIvosozKwAA
提取码:aeur - 本人还会不间断更新跟多的游戏开发