新建一个项目,在Assets里建立子文件夹并保存场景。首先建立一个Plane做为场景的地面并附上资源包里Background的材质,而后建立一个空的游戏对象,将其命名为Wall,在它的目录下建立4个Cube,调整它们的位置及大小以做为场景的墙体。接着建立一个Cube小方块,为了让小方块旋转起来,咱们须要编写一个脚本,代码以下:ide
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RotatePickUp : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { this.transform.Rotate(new Vector3(15,30,45)*Time.deltaTime); } }
建立一个Sphere小球,为它添加一个刚体Rigidbody,编写一个脚本控制小球的移动,代码以下:this
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PlayerController : MonoBehaviour { public float speed; private Rigidbody rb; public Text countText; public Text winText; private int count; // Use this for initialization void Start () { rb=GetComponent<Rigidbody>(); count=0; winText.text=""; SetCountText(); } void FixedUpdate() { float moveHorizontal=Input.GetAxis("Horizontal"); float moveVertical=Input.GetAxis("Vertical"); Vector3 movement=new Vector3(moveHorizontal,0,moveVertical); rb.AddForce(movement*speed); } void OnTriggerEnter(Collider other) { if(other.gameObject.CompareTag ("Pick Up")) { other.gameObject.SetActive(false); count+=1; SetCountText(); } } void SetCountText() { countText.text="Count:"+count.ToString(); if(count>=12) { winText.text="You Win"; } } // Update is called once per frame void Update () { } }
关于小球的参数设置:code
建立一个脚原本使摄像机跟着小球一块儿移动,代码以下:orm
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraController : MonoBehaviour { public GameObject player; private Vector3 offset; public GameObject pickupPfb; private GameObject[] obj1; private int objCount=0; // Use this for initialization void Start () { offset=this.transform.position-player.transform.position; //自动生成12个小方块 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 () { } }
关于摄像机的参数设置:对象