unity中的坐标变换API介绍

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Fenglele_Fans/article/details/82227541

unity中的游戏对象的世界坐标和自身坐标相互转换的API,在项目中看到这个方法有点疑惑,测试了一下,个人感觉用处不大,但还是记录下;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransformPointTest : MonoBehaviour {

    public Transform Cube;
    void Start ()
    {
        //cube.transformPoint()的含义就是把相对于cube的自身坐标为(2,0,0)转换成世界坐标
        //cube坐标(152,60,5),aa相对于cube的局部坐标是(2,0,0),所以aa的世界坐标就是(154,60,5);
        //返回位置受游戏对象缩放比例影响,此处缩放比例是 1 
        // Vector3 aa= Cube.TransformPoint(2, 0, 0);
        // print(aa);

        //将世界坐标(2,0,0)转换成以cube为坐标系的局部坐标
        //cube坐标(5,20,110),那么bb坐标就是(-3,-20,-110)
        //返回位置受游戏对象缩放比例影响,此处缩放比例是 1 
       // Vector3 bb =  Cube.InverseTransformPoint(2,0,0);
       // print(bb);

    }

    // Update is called once per frame
    void Update ()
    {
        TestMove();
    }
    void TestMove()
    {
        if (Input.GetKey(KeyCode.H))
        {
            //沿着cube自身坐标的forword方向移动
            Cube.Translate(Vector3.forward);
        }
        if (Input.GetKey(KeyCode.K))
        {
            //沿着cube世界坐标的back方向移动
            //transform.transformDirection(direction) 变换方向从局部坐标转换到世界坐标
            //不受缩放影响,返回的向量和direction有同样的长度(没有说有同样的方向)
            Cube.Translate(Cube.TransformDirection(Vector3.forward));
        }
        if (Input.GetKey(KeyCode.B))
        {
            //transform.InverseTransformDirection(direction)
            //变换方向从世界坐标转换到局部坐标
            Vector3 cc = transform.InverseTransformDirection(0,0,1);
            print(cc); //输出(1,0,0)
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Fenglele_Fans/article/details/82227541
今日推荐