栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

Unity 小游戏-打砖块

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity 小游戏-打砖块

效果演示

1.创建墙

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

 

方便以后对墙进行修改

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

 

 

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

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();//得到生成子弹的刚体组件
            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.检验

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/900577.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号