通过键盘上↑、↓、←、→实现对物体的控制
using System.Collections; using System.Collections.Generic; using UnityEngine;public class Gary_Text : MonoBehaviour {public Transform WuTi;public float speed = 1;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {//上if(Input.GetKey(KeyCode.UpArrow)){transform.eulerAngles = new Vector3(0, 0, 0);transform.position += transform.forward * speed;}//下else if (Input.GetKey(KeyCode.DownArrow)){transform.eulerAngles = new Vector3(0, 180, 0);transform.position += transform.forward * speed;}//左else if (Input.GetKey(KeyCode.LeftArrow)){transform.eulerAngles = new Vector3(0, 270, 0);transform.position += transform.forward * speed;}//右else if (Input.GetKey(KeyCode.RightArrow)){transform.eulerAngles = new Vector3(0, 90, 0);transform.position += transform.forward * speed;}} }
实现过程
GameObject->3D Object->Sphere创建一个Sphere作为被移动物体对象
在Sphere物体上绑定Gary_Text.cs脚本
可以在工程界面修改游戏速度speed值,默认是1
//引用控制移动物体脚本public Transform WuTi;//设置物体移动的速度,设置为public使在游戏界面可控制public float speed = 1;
在Updata()中跟新物体的坐标
void Update () {//上if(Input.GetKey(KeyCode.UpArrow)){transform.eulerAngles = new Vector3(0, 0, 0);transform.position += transform.forward * speed;}//下else if (Input.GetKey(KeyCode.DownArrow)){transform.eulerAngles = new Vector3(0, 180, 0);transform.position += transform.forward * speed;}//左else if (Input.GetKey(KeyCode.LeftArrow)){transform.eulerAngles = new Vector3(0, 270, 0);transform.position += transform.forward * speed;}//右else if (Input.GetKey(KeyCode.RightArrow)){transform.eulerAngles = new Vector3(0, 90, 0);transform.position += transform.forward * speed;}}
======分割线Σ(= =!)...=======
工具栏Edit->Project Settings ->Input可打开输入设备管理器对用户
Horizeontal -> 监听物体水平移动
Vertival ->监听物体竖直移动
。。。。。。
还很多,要学习