一、 介绍
玩家控制挡板移动,接住来回弹跳的小球。
小球掉下后,重新生成。
小球击毁所有障碍后,游戏成功。
二、 制作墙壁
切割图片片后,添加2d多边形碰撞体
新建2d物理材质,控制弹力,把材质拖入小球刚体组件中
三、 制作挡板
添加碰撞体、刚体
clamp函数控制左右移动限制
控制小球的发射点
控制发射速度
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class controler: MonoBehaviour
{
public Transform ballPos; // 球的初始位置
public GameObject ballPrefab; // 球的预制体对象
GameObject currentBall; // 当前球对象
private Rigidbody2D rb; // 玩家对象的刚体组件
public float moveSpeed = 5.0f; // 玩家移动速度
public float ballSpeed = 5.0f; // 球的发射速度
bool isPlaying = false; // 游戏是否正在进行中
void Start()
{
rb = GetComponent<Rigidbody2D>(); // 获取玩家对象的刚体组件
SpawnNewBall(); // 生成新球对象
}
void Update()
{
float h = Input.GetAxis("Horizontal"); // 获取水平移动方向
rb.velocity = Vector2.right * h * moveSpeed; // 设置玩家水平移动速度
Vector2 pos = transform.position; // 获取当前位置
pos.x = Mathf.Clamp(pos.x, -2.8f, 2.8f); // 限制玩家移动范围
transform.position = pos; // 设置玩家位置
if (Input.GetKeyDown(KeyCode.Space)) // 如果按下空格键
{
if (Input.GetKeyDown(KeyCode.Space) && !isPlaying) // 如果游戏没有进行中
{
isPlaying = true; // 设置游戏状态为进行中
ShootBall(); // 发射球
}
}
}
public void SpawnNewBall()
{
isPlaying =false; // 重置游戏状态为未开始
currentBall = Instantiate(ballPrefab, ballPos.position, ballPrefab.transform.rotation); // 生成新球对象
currentBall.transform.parent = transform; // 将新球对象设置为玩家的子对象
currentBall.GetComponent<Rigidbody2D>().isKinematic=true; // 禁用新球对象的物理模拟
}
void ShootBall()
{
currentBall.transform.parent = null; // 解除新球对象与玩家对象的父子关系
currentBall.GetComponent<Rigidbody2D>().isKinematic = false; // 启用新球对象的物理模拟
currentBall.GetComponent<Rigidbody2D>().velocity = new Vector2(Random.Range(-1f, 1f), 5f).normalized*ballSpeed; // 设置新球对象的发射速度和方向
}
}
四、 制作小球
添加原型碰撞体,刚体
五、 制作边界
球掉下去后,重新生成
添加碰撞器
void OnTriggerEnter2D(Collider2D col)
{
Destroy(col.gameObject);
FindObjectOfType<controler>().SpawnNewBall();
}
六、 制作动画
拖拽几个图片,形成简单动画
添加事件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class animation : MonoBehaviour
{
public void DestroyAfterAnimation(){
Destroy(gameObject);
}
}
七、 制作障碍物
小球触碰三种障碍物:1.无法消除的挡板 2撞一下消失的挡板 3撞两下消失的挡板
挡板消除发生爆炸
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class blocksnew : MonoBehaviour
{
// Start is called before the first frame update
public bool isGoldBlock;
public int maxHp = 1;
int hp;
public GameObject explo;
void Start ()
{
hp = maxHp;
}
void OnCollisionEnter2D(Collision2D col)
{
if (isGoldBlock)
{
return;
}
hp--;
if(hp <= 0)
{
Destroy(gameObject);
Instantiate(explo,transform.position,Quaternion.identity);
GameObject.Find("holder").GetComponent<holder>().BlockGetDestroy();
}
}
}
八、 挡板容器
装入所有挡板
统计所有挡板数量
挡板全部消失,玩家获胜
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class holder : MonoBehaviour
{
public int blocksAmount;
void Start ()
{
foreach(Transform child in transform)
{
if(!child.GetComponent<blocksnew>().isGoldBlock)
{
blocksAmount++;
}
}
}
public void BlockGetDestroy()
{
blocksAmount--;
if(blocksAmount <= 0)
{
Debug.Log("YOU WIN!");
}
}
}