void FixedUpdate()
{
float x = Input.GetAxis("Horizontal");//获取水平轴输入
float y = Input.GetAxis("Vertical");//获取垂直轴输入
transform.Translate(Vector3.right * x * battlePlayerSpeed * Time.deltaTime, Space.World);
transform.Translate(Vector3.up * y * battlePlayerSpeed * Time.deltaTime, Space.World);
}
这里划重点transform.Translate
官网手册介绍为:
Transform.Translatepublic void Translate (Vector3 translation);
public void Translate (Vector3 translation, Space relativeTo= Space.Self);
描述根据 translation 的方向和距离移动变换。
如果 relativeTo 被省略或设置为 Space.Self,则会相对于变换的本地轴来应用该移动。(在场景视图中选择对象时显示的 X、Y 和 Z 轴。) 如果 relativeTo 为 Space.World,则相对于世界坐标系应用该移动。
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
void Update()
{
// 沿着物体本身z轴1单位/s走
// Move the object forward along its z axis 1 unit/second.
transform.Translate(Vector3.forward * Time.deltaTime);
// 这样写就是沿着世界坐标了
// Move the object upward in world space 1 unit/second.
transform.Translate(Vector3.up * Time.deltaTime, Space.World);
}
}
(*^_^*)



