UnityAPI學習之Transform組件基本使用

目錄

Transform組件

訪問與獲取

Transform的位置和旋轉信息

Transform局部坐標和旋轉信息的獲取

Transform的縮放與正方向

縮放(Scale)

正方向

Transform相關的查找方法

銷毀游戲物體


Transform組件

訪問與獲取

現在創建一個容器放置GrisGO物體的、Transform組件并輸出其名字

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NO5_Transform : MonoBehaviour
{public GameObject GrisGO;// Start is called before the first frame updatevoid Start(){Debug.Log(GrisGO.transform);Transform GrisTrans = GrisGO.transform;Debug.Log("GrisGO所掛載的物體的transform組件的名字為:" + GrisTrans);Debug.Log("GrisGO所掛載的游戲物體引用為" + GrisTrans.gameObject);Debug.Log("GrisGO下的子物體(Transform)個數為" + GrisTrans.childCount);}// Update is called once per framevoid Update(){}
}

Transform的位置和旋轉信息

示例1:獲取GrisGO的位置信息和旋轉信息

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NO5_Transform : MonoBehaviour
{public GameObject GrisGO;// Start is called before the first frame updatevoid Start(){//Debug.Log(GrisGO.transform);Transform GrisTrans = GrisGO.transform;/*Debug.Log("GrisGO所掛載的物體的transform組件的名字為:" + GrisTrans);Debug.Log("GrisGO所掛載的游戲物體引用為" + GrisTrans.gameObject);Debug.Log("GrisGO下的子物體(Transform)個數為" + GrisTrans.childCount);*/Debug.Log("GrisGO世界空間的坐標位置為" + GrisTrans.position);Debug.Log("GrisGO以四元數形式表示的旋轉是" + GrisTrans.rotation);}// Update is called once per framevoid Update(){}
}

示例2:

在以上實例中,獲取的旋轉信息是以四元數的形式表示出來(如在rotation屬性中,x=1.58,但實際輸出的四元數為0.01379),現在要將rotation屬性以其原本的數值(歐拉角/度)形式展現出來

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NO5_Transform : MonoBehaviour
{public GameObject GrisGO;// Start is called before the first frame updatevoid Start(){//Debug.Log(GrisGO.transform);Transform GrisTrans = GrisGO.transform;/*Debug.Log("GrisGO所掛載的物體的transform組件的名字為:" + GrisTrans);Debug.Log("GrisGO所掛載的游戲物體引用為" + GrisTrans.gameObject);Debug.Log("GrisGO下的子物體(Transform)個數為" + GrisTrans.childCount);*/Debug.Log("GrisGO世界空間的坐標位置為" + GrisTrans.position);Debug.Log("GrisGO以四元數形式表示的旋轉是" + GrisTrans.rotation);Debug.Log("GrisGO以歐拉角/度的形式表示旋轉結果為" + GrisTrans.eulerAngles);}// Update is called once per framevoid Update(){}
}

Transform局部坐標和旋轉信息的獲取

局部坐標:子級相對于父級的位置信息

示例:輸出GrisGO的局部坐標和旋轉歐拉角

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NO5_Transform : MonoBehaviour
{public GameObject GrisGO;// Start is called before the first frame updatevoid Start(){//Debug.Log(GrisGO.transform);Transform GrisTrans = GrisGO.transform;/*Debug.Log("GrisGO所掛載的物體的transform組件的名字為:" + GrisTrans);Debug.Log("GrisGO所掛載的游戲物體引用為" + GrisTrans.gameObject);Debug.Log("GrisGO下的子物體(Transform)個數為" + GrisTrans.childCount);*/Debug.Log("GrisGO世界空間的坐標位置為" + GrisTrans.position);Debug.Log("GrisGO局部坐標為" + GrisTrans.localPosition);Debug.Log("GrisGO以四元數形式表示的旋轉是" + GrisTrans.rotation);Debug.Log("GrisGO以歐拉角/度的形式表示旋轉結果為" + GrisTrans.eulerAngles);Debug.Log("GrisGO的局部歐拉角/度的形式表示旋轉結果為" + GrisTrans.localEulerAngles);}// Update is called once per framevoid Update(){}
}

Transform的縮放與正方向

縮放(Scale)

對于Scale屬性,只有局部縮放LocalScale

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NO5_Transform : MonoBehaviour
{public GameObject GrisGO;// Start is called before the first frame updatevoid Start(){//Debug.Log(GrisGO.transform);Transform GrisTrans = GrisGO.transform;/*Debug.Log("GrisGO所掛載的物體的transform組件的名字為:" + GrisTrans);Debug.Log("GrisGO所掛載的游戲物體引用為" + GrisTrans.gameObject);Debug.Log("GrisGO下的子物體(Transform)個數為" + GrisTrans.childCount);*//*Debug.Log("GrisGO世界空間的坐標位置為" + GrisTrans.position);Debug.Log("GrisGO局部坐標為" + GrisTrans.localPosition);Debug.Log("GrisGO以四元數形式表示的旋轉是" + GrisTrans.rotation);Debug.Log("GrisGO以歐拉角/度的形式表示旋轉結果為" + GrisTrans.eulerAngles);Debug.Log("GrisGO的局部歐拉角/度的形式表示旋轉結果為" + GrisTrans.localEulerAngles);*/Debug.Log("GrisGO的局部縮放為" + GrisTrans.localScale);}// Update is called once per framevoid Update(){}
}

正方向

正方向即關于xyz軸所指的方向,其中分為有相對于世界空間的正方向和本地正方向(若無父級,則本地正方向與世界正方向相同),根據實際開發中的需求不同,調整相應的世界空間或者本地正方向的數值大小

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NO5_Transform : MonoBehaviour
{public GameObject GrisGO;// Start is called before the first frame updatevoid Start(){//Debug.Log(GrisGO.transform);Transform GrisTrans = GrisGO.transform;/*Debug.Log("GrisGO所掛載的物體的transform組件的名字為:" + GrisTrans);Debug.Log("GrisGO所掛載的游戲物體引用為" + GrisTrans.gameObject);Debug.Log("GrisGO下的子物體(Transform)個數為" + GrisTrans.childCount);*//*Debug.Log("GrisGO世界空間的坐標位置為" + GrisTrans.position);Debug.Log("GrisGO局部坐標為" + GrisTrans.localPosition);Debug.Log("GrisGO以四元數形式表示的旋轉是" + GrisTrans.rotation);Debug.Log("GrisGO以歐拉角/度的形式表示旋轉結果為" + GrisTrans.eulerAngles);Debug.Log("GrisGO的局部歐拉角/度的形式表示旋轉結果為" + GrisTrans.localEulerAngles);*////Debug.Log("GrisGO的局部縮放為" + GrisTrans.localScale);Debug.Log("GrisGO關于x軸的正方向為" + GrisTrans.right);Debug.Log("GrisGO關于y軸的正方向為" + GrisTrans.up);Debug.Log("GrisGO關于z軸的正方向為" + GrisTrans.forward);}// Update is called once per framevoid Update(){}
}

Transform相關的查找方法

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NO5_Transform : MonoBehaviour
{public GameObject GrisGO;// Start is called before the first frame updatevoid Start(){//Debug.Log(GrisGO.transform);Transform GrisTrans = GrisGO.transform;/*Debug.Log("GrisGO所掛載的物體的transform組件的名字為:" + GrisTrans);Debug.Log("GrisGO所掛載的游戲物體引用為" + GrisTrans.gameObject);Debug.Log("GrisGO下的子物體(Transform)個數為" + GrisTrans.childCount);*//*Debug.Log("GrisGO世界空間的坐標位置為" + GrisTrans.position);Debug.Log("GrisGO局部坐標為" + GrisTrans.localPosition);Debug.Log("GrisGO以四元數形式表示的旋轉是" + GrisTrans.rotation);Debug.Log("GrisGO以歐拉角/度的形式表示旋轉結果為" + GrisTrans.eulerAngles);Debug.Log("GrisGO的局部歐拉角/度的形式表示旋轉結果為" + GrisTrans.localEulerAngles);*////Debug.Log("GrisGO的局部縮放為" + GrisTrans.localScale);Debug.Log("GrisGO關于x軸的正方向為" + GrisTrans.right);Debug.Log("GrisGO關于y軸的正方向為" + GrisTrans.up);Debug.Log("GrisGO關于z軸的正方向為" + GrisTrans.forward);//查找Debug.Log("當前腳本掛載的游戲對象下的叫Gris的子對象身上的Transform組件是" + transform.Find("Gris"));//索引Debug.Log("當前腳本掛載的游戲對象下的叫Gris的子對象身上的Transform組件是" + transform.GetChild(0));Debug.Log("Gris當前在此父對象同級里所在的索引位置" + GrisTrans.GetSiblingIndex());}// Update is called once per framevoid Update(){}
}

銷毀游戲物體

Transform.Destroy(GrisGO);

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/20976.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/20976.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/20976.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

操作系統的分類

Linux類系統的組成 Linux操作系統Linux內核Linux應用 Linux內核是什么? Linux系統內核是構成Linux操作系統核心的部分,它是操作系統中最基礎和關鍵的組件,直接與硬件交互并管理計算機系統的底層資源。以下是Linux內核主要特性和功能的概覽…

一起學習大模型 - langchain里的 PromptTemplate詳細介紹

系列文章目錄 一起學習大模型 - 大模型的交互工具prompt簡介與運用 一起學習大模型 - langchain里的PromptTemplate詳細介紹 一起學習大模型 - langchain里PromptTemplate錯誤排查總結 文章目錄 系列文章目錄前言一、 安裝 LangChain二、 基本用法1. 導入庫并定義模板2. 填充…

API接口通道如何設置?

API接口通道如何設置? 如果分站點的AI接口使用openai(站點后臺->系統配置->AI參數配置->AI接口),則需要在超管后臺配置接口通道,其他方式則無需在超管后臺配置接口通道 1、進入超管后臺選擇接口通道&#x…

一鍵批量轉換,高效輕松管理:解鎖不同格式圖片統一處理新體驗,讓圖片管理更高效

在信息爆炸的時代,圖片管理成為了一個不容忽視的問題。我們時常面臨各種格式的圖片文件,不同的格式不僅增加了管理的難度,還可能導致兼容性問題。如何快速高效地管理不同格式的圖片,成為了現代人面臨的一大挑戰。現在,…

網上幫別人開網店賣貨的騙局!

小紅書幫別人開店賣貨的騙局主要涉及到一些不法分子利用小紅書平臺的流量和用戶信任度,通過虛假宣傳、承諾高額利潤等手段,誘騙用戶開店并**所謂的“賺錢機會”。 這些騙局往往以“輕松創業、快速致富”為誘餌,吸引那些對創業充滿熱情但缺乏經…

Redis常用命令——List篇

提到List,我們第一時間想到的就是鏈表。但是在Redis中,List更像是一種雙端隊列,例如C中的deque。它可以快速高效的對頭部和尾部進行插入和刪除操作。本片文章主要對List列表的相關命令進行詳解,希望本篇文章會對你有所幫助。 文章…

MedSegDiff-V2: Diffusion-Based Medical Image Segmentation with Transformer 論文總結

標題:MedSegDiff-V2: Diffusion-Based(基于擴散模型)Medical Image Segmentation(醫學圖像分割)with Transformer 論文(AAAI):https://ojs.aaai.org/index.php/AAAI/article/view/28…

【避坑全攻略】如何讓私人的LLM擁有一個嗓子——ChatTTS

OpenAI 發布 GPT4o 之后,使得越來越多的人都開始幻想屬于自己的AI“伴侶”,這最讓人驚艷的就是他們出色的TTS技術。而在此之前,主流的開源TTS有 XTTS 2 和 Bark。而近日,一個名為 ChatTTS 文本轉語音項目爆火出圈,引來…

Python中的random.choices詳解

1. 什么是random.choices函數? random.choices是Python標準庫中random模塊提供的一個函數,用于從給定的序列中隨機選擇一個值。這個函數可以用于實現隨機抽樣、按照概率進行選擇等功能。 random.choices(population, weightsNone, *, cum_weightsNone,…

.gitignore 文件

一.什么是 .gitignore 文件 在任何當前工作的 Git 倉庫中,每個文件都是這樣的: 追蹤的(tracked)- 這些是 Git 所知道的所有文件或目錄。這些是新添加(用 git add 添加)和提交(用 git commit 提…

汽美汽修店管理系統會員小程序的作用是什么

汽車后市場汽美汽修賽道同樣存在著大量商家,連鎖品牌店或個人小店等,門店扎堆且區域覆蓋面積廣,當然每天車來車往也有不少生意。 隨著線上化程度加深和商家不斷拓展市場的需要,傳統運營模式可能難以滿足現狀,尤其是年…

Element - UI <el-table-column>多選數據提交后禁用已提交的多選框

1. 通過 selection-change"selectionChange" 將已選擇的數據存入selectData數組中 <el-table :data"tableData" class"my-5" selection-change"selectionChange" > //多選框已選擇的數據 const selectData ref([]); const sel…

在線圖片測試用例

開發中經常需要mock圖片&#xff0c;下面是可用于測試的在線圖片URL示例&#xff0c; https://picsum.photos/200/200?random2 https://picsum.photos/200/200?random1 https://picsum.photos/300/200?random2 https://picsum.photos/300/200?random1 說明&#xff1a…

前端環境配置(后端使用前端版,簡易版非專業前端)

聲明本人不是專業做前端的&#xff0c;只是平常開發有時候需要運行前端代碼&#xff0c;記錄一下配置環境。 安裝nvm nvm 即 (node version manager)&#xff0c;好處是方便切換 node.js 版本。 自己網上下載安裝包即可&#xff0c;傻瓜式安裝。 安裝注意事項 要卸載掉現有的 …

HALCON-從入門到入門-圖像格式的互相轉換

1.廢話 上次說到了圖片的讀取和寫入到本地&#xff0c;這次說一下圖片的格式相關。 位圖和矢量圖 photoshop處理出來的圖片肯定叫做圖片&#xff0c;那么coreDraw處理出來的圖片是不是也叫圖片。 之間就有區分&#xff0c;一種叫做位圖&#xff0c;一種叫做矢量圖 位圖和矢…

AI大模型探索之路-實戰篇13: 從對話到報告:打造能記錄和分析的Agent智能數據分析平臺

系列篇章&#x1f4a5; AI大模型探索之路-實戰篇4&#xff1a;深入DB-GPT數據應用開發框架調研 AI大模型探索之路-實戰篇5&#xff1a;探索Open Interpreter開放代碼解釋器調研 AI大模型探索之路-實戰篇6&#xff1a;掌握Function Calling的詳細流程 AI大模型探索之路-實戰篇7…

echarts 圖表不顯示的問題

是這樣的&#xff0c;點擊詳情&#xff0c;再點擊統計&#xff0c;切換的時候就不會顯示echarts圖表&#xff0c;剛開始使用的是next Tick&#xff0c;沒有使用定時器&#xff0c;后來加上了定時器就實現了如下所示&#xff1a; 代碼是如下 const chartContainer ref(null); …

【面試題-011】如何設計一個三高系統

設計一個“三高”系統&#xff08;即高可用、高性能、高并發&#xff09;需要綜合考慮系統架構、技術選型、運維管理等多個方面。以下是一些關鍵的設計原則和步驟&#xff1a; 1. 確定系統需求和目標 高可用&#xff1a;系統需要能夠承受故障&#xff0c;并在故障發生時快速恢…

【Text2SQL 論文】DBCopilot:將 NL 查詢擴展到大規模數據庫

論文&#xff1a;DBCopilot: Scaling Natural Language Querying to Massive Databases ???? Code: DBCopilot | GitHub 一、論文速讀 論文認為目前的 Text2SQL 研究大多只關注具有少量 table 的單個數據庫上的查詢&#xff0c;但在面對大規模數據庫和數據倉庫的查詢時時卻…

618商品網頁制作編程示例開發案列優質學習資料資源工具與案列應用場景開發文檔教程資料】

創建一個簡單的商品網頁可以用HTML、CSS和JavaScript來實現。這種網頁會包括商品的圖片、名稱、描述、價格和購買按鈕等。下面是一個詳細的源碼案例及其講解&#xff1a; 1. 文件結構 假設我們有以下文件結構&#xff1a; /product-page/imagesproduct.jpgindex.htmlstyle.c…