您的位置:首页 > 移动开发 > Unity3D

Unity打砖块小游戏制作过程

2020-03-22 07:26 585 查看

3 月,跳不动了?>>>

操作步骤

  1. 新建一个plane,修改位置坐标为(0,0,0)导入资源包, 新建一个空对象,命名为Wall,用于存放后面脚本生产的小方块
  2. 新建一个脚本,重命名为Brick,用于生成由Cube组成的墙。把脚本附给Wall,脚本复制对象选择资源包中的小方块的预制件 。脚本内容如下:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Brick : MonoBehaviour
    {
    public GameObject brick;
    private int columnNum=8;//列数
    private int rowNum = 6;//行数
    // Start is called before the first frame update
    void Start()
    {
    for(int i = 0; i < rowNum; i++)
    {
    for(int j = 0; j < columnNum; j++)
    {
    Instantiate(brick, new Vector3(j-5,i),Quaternion.identity);
    }
    }
    }
    
    // Update is called once per frame
    void Update()
    {
    
    }
    }
     
  3. 新建一个脚本文件,命名为shoot,用于生成发射的小球,将脚本附给Main Camrea,发射位置选择摄像机的位置,复制对象选择资源包中的小球的预制件,脚本内容如下:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Shoot : MonoBehaviour
    {
    public GameObject shootPosition;
    private float force = 1000;
    public Rigidbody shooter;
    private float cameraSpeed = 0.1f;
    // Start is called before the first frame update
    void Start()
    {
    
    }
    
    // Update is called once per frame
    void Update()
    {
    Rigidbody ball;
    if (Input.GetKeyDown(KeyCode.Space))
    {
    ball = Instantiate(shooter, shootPosition.transform.position, Quaternion.identity) as Rigidbody;
    ball.AddForce(force * ball.transform.forward);
    
    }
    if (Input.GetKey(KeyCode.LeftArrow))
    {
    this.transform.Translate(Vector3.left * cameraSpeed);
    }
    if (Input.GetKey(KeyCode.RightArrow))
    {
    this.transform.Translate(Vector3.right * cameraSpeed);
    }
    if (Input.GetKey(KeyCode.UpArrow))
    {
    this.transform.Translate(Vector3.up * cameraSpeed);
    }
    if (Input.GetKey(KeyCode.DownArrow))
    {
    this.transform.Translate(Vector3.down * cameraSpeed);
    }
    }
    }

     

5. 制作完成

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: