1、首先打开Unity,创建一个plane和一个cube物体,plane作为cube物体移动的载体。
2、分别选中plane和cube点击Reset进行重设其位置。
3、将cube的y轴高度设为0.5,即正好位于平面上。
4、选中cube,并点击右边Inspector面板的add component,并输入脚本名字为Move。
5、点击creat and add进行创建。
6、点击脚本的设置按钮并选择Edit Script打开脚本。
7、按照下图所示添加如下蜣贺鱿柢代码:publicfloatmovespeed = 5;publicGameObject go;if(Input.GetKey(KeyCode.W)像粜杵泳){go.transform.Translate( 0, 0, movespeed * Time.deltaTime, Space.World);}if(Input.GetKey(KeyCode.S)){go.transform.Translate( 0, 0, movespeed * Time.deltaTime * (-1),Space.World);}if(Input.GetKey(KeyCode.A)){go.transform.Translate(movespeed * Time.deltaTime*(-1), 0, 0, Space.World);}if(Input.GetKey(KeyCode.D)){go.transform.Translate(movespeed * Time.deltaTime, 0, 0, Space.World);}
8、点击附加到Unity或者按下键盘ctrl+S,接着回到Unity界面。
9、选中cube,并按照图示拖入Go方框中。
10、点击运行,开始测试。
11、可以发现游戏物体cube已经可以进行前后左右的移动。