Unity3D——Transform类

世界坐标 position:transform.position,世界坐标中的位置,绝对坐标。 局部坐标 localPosition:transform.localPosition,相对于父对象的位置,相对坐标。如果一个物体没有父对象,二者等效。eulerAngles:世界坐标系中的旋转(欧拉角)。localEulerAngles:相对于父级的变换旋转角度。right:世界坐标系中的右...
摘要由CSDN通过智能技术生成

以下运行脚本挂在“Image”上

属性(成员变量)

position:transform.position,世界坐标系中的位置,绝对坐标。
localPosition:transform.localPosition,相对于父对象的位置,相对坐标,图中的1的x、y、z既是该对象的相对坐标。如果一个物体没有父对象,二者等效。
例:做垂直或水平匀速移动

    private float movementSpeed = 5f;

    void Update()
    {
        //get the Input from Horizontal axis
        float horizontalInput = Input.GetAxis("Horizontal");
        //get the Input from Vertical axis
        float verticalInput = Input.GetAxis("Vertical");

        //update the position
        transform.position = transform.position + new Vector3(horizontalInput * movementSpeed * Time.deltaTime, verticalInput * movementSpeed * Time.deltaTime, 0);

        //output to log the position change
        Debug.Log(transform.position);
    }

eulerAngles:世界坐标系中的欧拉角(以度为单位)。transform.eulerAngle = new Vector3(0.0f,0.0f,0.0f),围绕x、y、z轴做旋转。
localEulerAngles:相对于父级的变换欧拉角。
例:围绕x、y、z轴做旋转

    float rotationSpeed = 45;
    Vector3 currentEulerAngles;
    float x;
    float y;
    float z;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.X)) x = 1 - x;
        if (Input.GetKeyDown(KeyCode.Y)) y = 1 - y;
        if (Input.GetKeyDown(KeyCode.Z)) z = 1 - z;

        //modifying the Vector3, based on input multiplied by speed and time
        currentEulerAngles += new Vector3(x, y, z) * Time.deltaTime * rotationSpeed;

        //apply the change to the gameObject
        transform.eulerAngles = currentEulerAngles;
    }

rotation:世界坐标系中的旋转(四元数)。transform.rotation=Quaternion.Euler(0.0f,0.0f,0.0f);在世界坐标系中物体变换的旋转角度作为Quaternion

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值