软件编程
位置:首页>> 软件编程>> C#编程>> Unity实现打砖块游戏

Unity实现打砖块游戏

作者:大聪明深夜a题  发布时间:2023-12-06 04:45:04 

标签:unity,打砖块

本文实例为大家分享了Unity实现打砖块游戏的具体代码,供大家参考,具体内容如下

效果演示

Unity实现打砖块游戏

1.创建墙

1.1我们用预制体来统一管理墙

Unity实现打砖块游戏

方便以后对墙进行修改

1.2我们还需要给砖块一个刚体组件(物理属性),不然墙就固定在那里不动。

Unity实现打砖块游戏

1.3 把砖块弄出来 再弄成一堵墙

Unity实现打砖块游戏

2.发射 *

我们将 * 也用预制体的方式创造。

这时就到了我们写代码的时候了。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class shoot : MonoBehaviour
{
    public GameObject bullet;//定义游戏物体 ( * )
    float speed = 30;// * 速度
    // Start is called before the first frame update
    void Start()
    {
       
    }
 
    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))//是否按下鼠标左键
        {
            
           GameObject b=GameObject.Instantiate(bullet,transform.position,transform.rotation);//生成 * 的位置
            Rigidbody rd = b.GetComponent<Rigidbody>();//得到生成 * 的刚体组件
            rd.velocity = transform.forward * speed;//为 * 施加速度
        }
    }
}

将这段代码给摄像机就可以在运行游戏时按下鼠标左键发射 *

3.移动发射

将以下脚本代码给摄像机就可以完成移动了

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class hv: MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
 
    // Update is called once per frame
    void Update()
    {
        float h=Input.GetAxis("Horizontal");//水平方向
        float v = Input.GetAxis("Vertical");//垂直方向
        transform.Translate(new Vector3(h, v, 0)/60);//移动速度
    }
}

4.检验

Unity实现打砖块游戏

来源:https://blog.csdn.net/qq_62045667/article/details/122520507

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com