新建一个立方体Cube,在立方体上挂上该脚本:
using UnityEngine; using System.Collections; public class MoveDir : MonoBehaviour { // Use this for initialization void Start () { // rigidbody.AddForce(new Vector3(0, 0, 500)); //这个是给物体一个向前的力,使其运动 } void Update () { //每帧都会执行一次,详情可以看Unity3d圣典 switch (Input.inputString) { case "w": transform.Translate(new Vector3(0,0,5*Time.deltaTime)); //Time.deltaTime代表的是上一次执行 Update()与现在的时间差 break; case "a": transform.Translate(new Vector3(-5*Time.deltaTime, 0, 0)); break; case "s": transform.Translate(new Vector3(0,0, -5*Time.deltaTime)); break; case "d": transform.Translate(new Vector3(5*Time.deltaTime, 0, 0)); break; } } }
点击运行后,就可以用上下左右来控制立方体了
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>