unity学习,希望我的博客能给喜欢unity的朋友带来帮助
今天没讲新内容,复习了几个小项目,我重新做了一个滚动的盒子。
滚动的盒子,要实现的功能就是点击开始后,立方体会原地滚动,小球会随着键盘的按键左右前后移动,与立方体发生碰撞后立方体会消失,并显示吃掉了第几个食物,当立方体全部消失后,会显示胜利了。
游戏界面如图所示:
代码如下:
- using UnityEngine;
- using System.Collections;
- public class sphere : MonoBehaviour {
- public float speed = 5f;
- // Use this for initialization
- void Start () {
-
- }
-
- // Update is called once per frame
- void Update () {
- float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
- float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
- transform.Translate(new Vector3(x, 0, z));
- }
- int i = 0;
- void OnTriggerEnter(Collider col)
- {
- if (col.gameObject.tag == "food")
- {
- GameObject.Destroy(col.gameObject);
- i++;
- print("吃掉第" + i + "个食物");
- if (i == 8)
- {
- print("胜利了!!");
- }
- }
- }
- }
更多精彩请点击 http://www.gopedu.com/article