unity打方块游戏

本文介绍了如何使用Unity创建一款2D弹球游戏,包括制作墙壁、挡板、小球、边界、动画、障碍物以及游戏胜利条件。游戏玩法是控制挡板接住弹跳的小球,击毁所有障碍物以赢得游戏。同时,文章提供了关键代码示例来说明游戏逻辑和物理交互的实现。
摘要由CSDN通过智能技术生成


一、 介绍

玩家控制挡板移动,接住来回弹跳的小球。
小球掉下后,重新生成。
小球击毁所有障碍后,游戏成功。

在这里插入图片描述


二、 制作墙壁

切割图片片后,添加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!");
		}
	}
}


九、 下载素材图片

在这里插入图片描述


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忽然602

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值