1、建立一个3D工程
依次 File -> New Project

Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
2、在Hierarchy视图中创建小球 点击Create -> 3D Object ->Sphere ,命名为Ball,并且在右面窗口点击Inspector,修改Ball的基本属性
 Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
点击Ball 在右面视图中,点击Inspector,添加一个Rigidbody组件 ,并配置Rigidbody的属性,其中Mass(质量)属性设置为0.1,这里勾选了Use Gravity 选项框,表明该Ball对象会受到重力影响而下落。
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

3、在Hierarchy视图中添加一个Cube对象,重命名为Plate,该对象的属性如图:
Image may be NSFW.
Clik here to view.
4、给Ball创建脚本 ,命名为BallController,双击,进行脚本语言编译。代码如下
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallController : MonoBehaviour {
public float thrust = 40.0f;//用来控制球与盘子碰撞时受力的大小。
private Rigidbody rb;//用来存储Ball对象上添加的Rigidbody组件。
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
if(transform.position.y < -10)//当球下落到y值小于-10,则销毁Ball对象并退出程序。
{
Destroy (gameObject);
Application.Quit ();
}
}
void OnCollisionEnter(Collision collision)//OnCollisionEnter 是Unit中处理碰撞体间碰撞的事件函数。
{
rb.AddForce (new Vector3 (Random.Range (-0.2f, 0.2f), 1.0f, 0) * thrust);
}
}
5、给Plate创建脚本,过程如上,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovePlate : MonoBehaviour {
// Use this for initialization
void Start () {
}
public float speed = 5.0f;//用来控制左右移动盘子的速度,默认值为5.0f。
// Update is called once per frame
void Update () {
float h = Input.GetAxis ("Horizontal");//表示得到水平方向的输入。可以通过左右方向键来控制移动方向,按下右方向键时函数返回正值,反之返回负值。
transform.Translate (Vector3.right * h * speed * Time.deltaTime);//transform.Translate表示移动脚本绑定的游戏对象,移动的距离由作为参数的表达式决定。
/*
* 此处之所以乘上Time.deltaTime,是因为在使用Update()时,对于一些变量,如速度、移动距离等通常需要乘以Time.deltaTime来抵消帧率带来的影响,使物体状态的改变看起来比较均匀正常。
*/
}
}
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
7、到此该游戏已经完成,运行结果如下:
Image may be NSFW.
Clik here to view.