Production process of Unity brick playing games

Operation steps

  1. Create a new plane, modify the location coordinate to (0, 0, 0) import the resource package,Create a new empty object named Wall, which is used to store the small blocks produced by the following script
  2. Create a new script, rename it Brick, to generate walls made of cubes. Attach the script to Wall and copy the object to select the prefabrication of the small blocks in the resource package. The script is as follows:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Brick : MonoBehaviour
    {
        public GameObject brick;
        private int columnNum=8;//Column number
        private int rowNum = 6;//Row number
        // 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. Create a new script file named shot, which is used to generate the launching ball. Attach the script to the Main Camrea, select the camera location for the launching location, and copy the object to select the prefabrication of the ball in the resource package. The content of the script is as follows:
    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. Production completed

Posted on Sat, 21 Mar 2020 07:05:24 -0700 by konky