Unity-Transform-坐标转换

世界坐标转本地坐标

print(Vector3.forward);

世界坐标系 转本地坐标系 可以帮助我们大概判断一个相对位置

世界坐标系的点 转换 为相对本地坐标系的点

//受到缩放影响

print("转换后的点 " + this.transform.InverseTransformPoint(Vector3.forward));

世界坐标系的方向 转换 为相对本地坐标系的方向

//不受缩放影响

print("转换后的方向" + this.transform.InverseTransformDirection(Vector3.forward));

//受缩放影响

print("转换后的方向(受缩放影响)" +this.transform.InverseTransformVector(Vector3.forward));

本地坐标转世界坐标

本地坐标系的点 转换 为相对世界坐标系的点 受到缩放影响

print("本地 转 世界 点" + this.transform.TransformPoint(Vector3.forward));

本地坐标系的方向 转换 为相对世界坐标系的方向

//不受缩放影响

print("本地 转 世界 方向" + this.transform.TransformDirection(Vector3.forward));

//受缩放影响

print("本地 转 世界 方向" + this.transform.TransformVector(Vector3.forward));

练习

cs 复制代码
public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        CreatBoll();
    }

  
    public void Creat()//在自己坐标系下的-1,0,1位置创建一个空物体
    {
        GameObject a = new GameObject("C");    //先转换成世界坐标的位置    
                                               //再在世界坐标的位置创建,下同

        a.transform.position = transform.TransformPoint(new Vector3(-1, 0, 1));

    }

    public void CreatBoll()//在自己坐标系下的3个位置创建三个球
    {

      GameObject a=  GameObject.CreatePrimitive(PrimitiveType.Sphere);
        a.transform.position = transform.TransformPoint(new Vector3(0, 0, 1)); 
        GameObject b = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        b.transform.position = transform.TransformPoint(new Vector3(0, 0, 2));
        GameObject c = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        c.transform.position = transform.TransformPoint(new Vector3(0, 0, 3));
    }
}
相关推荐
tntxia8 小时前
linux curl命令详解_curl详解
linux
扛枪的书生10 小时前
Linux 网络管理器用法速查
linux
顺风尿一寸13 小时前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux
XIAOHEZIcode19 小时前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录
linux·ubuntu·命令行
唐青枫21 小时前
别再只会用 cron:Linux systemd Timer 定时任务实战详解
linux
AlfredZhao3 天前
生产环境里,为什么不建议把普通端口直接暴露到公网?
linux·https·443·80
戴为沐4 天前
Linux内存扩容指南
linux
zylyehuo4 天前
Linux 彻底且安全地删除文件
linux
用户805533698035 天前
主线 U-Boot 上 RK3506:和闭源 rkbin 拔河的三个隐性契约
linux·嵌入式
用户034095297915 天前
linux fcitx 5 雾凇拼音 设置在中文输入法下仍然输入英文标点
linux