https://docs.unity3d.com/2018.3/Documentation/ScriptReference/Space.html
https://docs.unity3d.com/2018.3/Documentation/ScriptReference/Transform.Translate.html
1.Space.World与Space.Self
在Space.World与Space.Self下的旋转与移动
float Speed;
public bool isWorldSpace;
void Start()
{
Speed = 20.0f;
//transform.Rotate(60, 0, 60);//旋转
}
void Update()
{
//Rotate or Move the GameObject in World Space if in the isWorldSpace state
if (isWorldSpace)
transform.Rotate(Vector3.up * Speed * Time.deltaTime, Space.World);//绕世界坐标y轴旋转
//transform.Translate(Vector3.right * Speed * Time.deltaTime*0.05f,Space.World);//沿世界坐标x轴移动
//Otherwise, rotate the GameObject in local space
else
transform.Rotate(Vector3.up * Speed * Time.deltaTime, Space.Self);//绕自身坐标y轴旋转
//transform.Translate(Vector3.right * Speed * Time.deltaTime*0.05f, Space.Self);//沿自身坐标x轴移动
//Press the Space button to switch between world and local space states
if (Input.GetKeyDown(KeyCode.Space))//按空格键改变自身坐标或者世界坐标
{
//Make the current state switch to the other state
isWorldSpace = !isWorldSpace;
//Output the Current state to the console
Debug.Log("World Space : " + isWorldSpace.ToString());
}
}
private void OnGUI()
{
GUI.Box(new Rect(100, 100, 100, 100), isWorldSpace.ToString());//信息显示面板
}
World坐标系是固定的方向,不会随着物体的旋转而改变。
Self坐标系会随着自身的旋转而改变。
true表示World坐标系,false表示Self坐标系。
transform.Translate()
1.
public void Translate(Vector3 translation);
public void Translate(Vector3 translation, Space relativeTo = Space.Self);
Moves the transform in the direction and distance of translation.
If relativeTo is left out or set to Space.Self the movement is applied relative to the transform’s local axes. (the x, y and z axes shown when selecting the object inside the Scene View.) If relativeTo is Space.World the movement is applied relative to the world coordinate system.
public class ExampleClass : MonoBehaviour
{
void Update()
{
// 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);
}
}
2.
public void Translate(float x, float y, float z);
public void Translate(float x, float y, float z, Space relativeTo = Space.Self);
Moves the transform by x along the x axis, y along the y axis, and z along the z axis.
If relativeTo is left out or set to Space.Self the movement is applied relative to the transform’s local axes. (the x, y and z axes shown when selecting the object inside the Scene View.) If relativeTo is Space.World the movement is applied relative to the world coordinate system.
public class ExampleClass : MonoBehaviour
{
void Update()
{
// Move the object forward along its z axis 1 unit/second.
transform.Translate(0, 0, Time.deltaTime);
// Move the object upward in world space 1 unit/second.
transform.Translate(0, Time.deltaTime, 0, Space.World);
}
}
3.
public void Translate(Vector3 translation, Transform relativeTo);
Moves the transform in the direction and distance of translation.
The movement is applied relative to relativeTo’s local coordinate system. If relativeTo is null, the movement is applied relative to the world coordinate system.
public class ExampleClass : MonoBehaviour
{
void Update()
{
// Move the object to the right relative to the camera 1 unit/second.
transform.Translate(Vector3.right * Time.deltaTime, Camera.main.transform);
}
}
4.
public void Translate(float x, float y, float z, Transform relativeTo);
Moves the transform by x along the x axis, y along the y axis, and z along the z axis.
The movement is applied relative to relativeTo’s local coordinate system. If relativeTo is null, the movement is applied relative to the world coordinate system.
public class ExampleClass : MonoBehaviour
{
void Update()
{
// Move the object to the right relative to the camera 1 unit/second.
transform.Translate(Time.deltaTime, 0, 0, Camera.main.transform);
}
}