Unity Space.Word&self与Translate移动

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);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值