unity 打砖块—休闲小游戏,摸鱼必备(完整代码)

打砖块小游戏通常会被当做Unity学习的第一个案例,下面给大家介绍如何实现打砖块案例的

 

 建立cube,大体这个样子,可以建造自己喜欢的形状和颜色。

给正方形的cube添加刚体

 将摄像机的位置对准创建的物体

创建一个“子弹”,添加钢体,然后拖动到文件夹,变成预设体,删除原有的“子弹” 

我这里做成了一个炮弹的样式

 代码部分,把代码附着给摄像机

完整代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class asd : MonoBehaviour
{
    public float speed = 5;  //移动速度
    public float zidanspeed = 50;  //子弹发射速度
    private float x;  //x轴
    private float y; //y轴
    public GameObject zidan;  //物体


    //start函数中的代码,在游戏开始时会运行一次,后面将不会运行 ,这里用不到
    void Start()
    {
    }

    // Update 里面的代码 每一帧都会运行
    void Update()
    {
        //移动代码
        float x = Input.GetAxis("Horizontal"); 
        float y = Input.GetAxis("Vertical");    
        transform.Translate(new Vector3(x, y, 0) * Time.deltaTime * speed);
        //子弹触碰物体后销毁,代码
        if (Input.GetMouseButtonDown(0)) 
        {
            
            GameObject a = GameObject.Instantiate(zidan, transform.position, transform.rotation); 
            Rigidbody rgd = a.GetComponent<Rigidbody>();
            rgd.velocity = transform.forward * zidanspeed; 
            Destroy(a, 1);  //子弹一秒钟后消失
        }
    }
}

将子弹预设体拖动到“public GameObject zidan;  //物体”中

准星设置

猜你喜欢

转载自blog.csdn.net/weixin_59848198/article/details/125854166