Unity simulation ball rolling game

Product display

Operation steps

  1. Create a new plane as the Ground, rename it to Ground, and change the location coordinate to (0, 0, 0). Create another empty object, create a Cube in it, and copy it three times. Rename it to Wall as a Wall, and change the coordinates to (0, 0, 0). By changing the position and scale properties of the four cubes, make them around the Ground.
  2. Create a new Cube, rename it PickUp, attach the material, change the location coordinate to (0, 1, 0), and change the rotation attribute to tilt it. Add a Rigidbodt component to PickUp and tick the Use Gravity and is kinetic options to hold it in place.Create a new script file named RotatePickUp as a small box rotation script. The script content is:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class RotatePickUp : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            
        }
    
        // Update is called once per frame
        void Update()
        {
            this.transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
        }
    }
    

    Save the compilation, then copy 11 pickups and package them into an empty object.

  3. Create a new Sphere, rename it to Player, modify the location coordinate to (0, 0, 0), add a Rigidbody component, and check Use Gravity. Create a new script file named playercontroller to control the ball. At the same time, create two new texts, renamed CountText and WinText respectively, for counting and prompting. When the player controls the small ball to touch the small box, the small box will disappear, and the upper left corner count will be + 1. When a certain number is reached, it will display "You Win!" The script is as follows:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CameraContraller : MonoBehaviour
    {
        public GameObject player;
        private Vector3 offset;
        public GameObject PickUpPfb;
        private GameObject[] obj1;
        private int objCount = 0;
        // Start is called before the first frame update
        void Start()
        {
            offset=this.transform.position - player.transform.position;
            obj1 = new GameObject[12];
            for (objCount = 0; objCount < 12; objCount++)
            {
                obj1[objCount] = GameObject.Instantiate(PickUpPfb);
                obj1[objCount].name = "PickUp"+objCount.ToString();
                obj1[objCount].transform.position = new Vector3(4*Mathf.Sin(Mathf.PI/6*objCount),1,4*Mathf.Cos(Mathf.PI/6*objCount));
                
            }
        }
        void LateUpdate()
        {
            this.transform.position = player.transform.position + offset;
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
    }
    

     

  4. Attach the script to the corresponding components, compile and debug.

Tags: Attribute

Posted on Sat, 21 Mar 2020 10:08:27 -0400 by Rex__