private Transform m_Transform; //定义类型为Transform变量m_Transform
// Start is called before the first frame update
void Start()
{
m_Transform = gameObject.GetComponent<Transform>(); //通过transform组件进行gameobject实例
}
// Update is called once per frame
void Update()
{
//m_Transform.Translate(Vector3.forward*0.1f, Space.Self); //往前方走,行走速度同时降低到0.1,以自身坐标系
//m_Transform.Translate(Vector3.forward * 0.1f, Space.World); //往前方走,行走速度同时降低到0.1,以世界为基础坐标系
if (Input.GetKey(KeyCode.W)) //捕获键盘按键,按键W,脚本控制向前走,以世界为基础坐标系
{
// Debug.Log("forward");
m_Transform.Translate(Vector3.forward*0.1f,Space.World);
}
if (Input.GetKey(KeyCode.S)) //捕获键盘按键,按键S,脚本控制向后走,以世界为基础坐标系
{
// Debug.Log("back");
m_Transform.Translate(Vector3.back * 0.1f, Space.World);
}
if (Input.GetKey(KeyCode.A)) //捕获键盘按键,按键A,脚本控制向左走,以世界为基础坐标系
{
// Debug.Log("left");
m_Transform.Translate(Vector3.left * 0.1f, Space.World);
}
if (Input.GetKey(KeyCode.D)) //捕获键盘按键,按键D,脚本控制向右走,以世界为基础坐标系
{
// Debug.Log("right");
m_Transform.Translate(Vector3.right * 0.1f, Space.World);
}
if (Input.GetKey(KeyCode.Q)) //捕获键盘按键,按键Q,脚本控制向上走,以世界为基础坐标系
{
// Debug.Log("debug q");
m_Transform.Translate(Vector3.up * 0.1f,Space.World);
}
if (Input.GetKey(KeyCode.E)) //捕获键盘按键,按键E,脚本控制向下走,以世界为基础坐标系
{
m_Transform.Translate(Vector3.down*0.1f,Space.World);
}
}
Transform为当前游戏对象的Transform组件
gameobject为当前游戏对象的实例