unity easy game of making small balls and eating bricks

Posted by jminscoe on Tue, 05 May 2020 13:15:45 +0200

Download package, adjust basic settings

Create scene

Ground: create a plane as ground, double the magnification, and add the material.

Wall: create four cube s to add to an empty management, adjust the size of the position (10 units from the origin) (length 20.5, width 0.5)

PickUp: create a cube (length, width and height 0.5, Rotation45), add Tag -- PickUp (create a new Tag), add Rigidbody (tick Is Trigger and is kinetic)

Write a rotation script

void Update () {
        this.transform.Rotate(new Vector3(15, 30, 45)*Time.deltaTime);
	}

Put PickUp as a prefab into the scene

Method 1: drag into 12 PickUp to adjust the position in the scene to form a circle.

Method 2: write script to generate automatically (write in the script of camera, the following is the new content)

public class CameraController : MonoBehaviour {
    public GameObject pickupPfb;
    private GameObject[] obj1;//Generate an array
    private int objCount = 0;
	// Use this for initialization
	void Start () {
        obj1 = new GameObject[12];
        for (objCount = 0; objCount < 12; objCount++)
        {
            obj1[objCount] = GameObject.Instantiate(pickupPfb);
            obj1[objCount].name = "pickup" + objCount.ToString();//name
            obj1[objCount].transform.position = new Vector3(4*Mathf.Sin(Mathf.PI/6*objCount),1,4*Mathf.Cos(Mathf.PI/6*objCount));//Locate
        }
	}
}

Game method

Player: create a ball, set Tag to player, and add Rigidbody

Script of player movement

public class PlayerController : MonoBehaviour {
    public float speed;//Setting the speed of the slot can change the ball speed
    private Rigidbody rb;//Defining rigid body objects
	void Start () {
        rb = GetComponent<Rigidbody>();//Get the Rigidbody of the object
	}
	
    //Control the ball
    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");//Get the direction of Horizontal
        float moveVertical = Input.GetAxis("Vertical");//Get the direction of Vertical
        Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);//No movement on Y axis
        rb.AddForce(movement * speed);//Add a force
    }

    //Bricks disappear when they touch them
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("PickUp"))//When the object is labeled PickUp
        {
            other.gameObject.SetActive(false);//Object invisible
        }
    }

 

visual angle

Camera follows ball

public class CameraController : MonoBehaviour {
    public GameObject player;
    private Vector3 offset;//Offset between camera and player
	// Use this for initialization
	void Start () {
        offset = this.transform.position - player.transform.position;//Calculate offset
	}
	
    void LateUpdate()
    {
        this.transform.position = player.transform.position + offset;
    }

 

Game data

Create text in the game that can record game data

Create two texts

       

The script of Text is written in the script of Player (the following is the newly added part)

using UnityEngine.UI;

public class PlayerController : MonoBehaviour {
    public Text countText;
    public Text winText;
    private int count;//count
	void Start () {
        count = 0;
        winText.text = "";
        SetCountText();
	}
    //Bricks disappear when they touch them
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("PickUp"))//When the object is labeled PickUp
        {
            other.gameObject.SetActive(false);//Object invisible
            count += 1;//Add one to the brick count
            SetCountText();//One update at a time

        }
    }

    void SetCountText()//Custom function
    {
        countText.text = "Count:" + count.ToString();
        if (count >= 12)
        {
            winText.text = "You Win";
        }
    }
}

Video link: https://unity-1-1301924619.cos.ap-nanjing.myqcloud.com/Unity%202017.4.36c1%20Personal%20(64bit)%20-%20Rollball.unity%20-%20RollBall%20-%20PC%2C%20Mac%20%26%20Linux%20Standalone_%20_DX11_%202020-05-04%2018-22-07.mp4

Topics: Unity