簡體   English   中英

Unity3D:平移和旋轉后將子對象帶入其原始 position

[英]Unity3D: Bring child objects in it's original position after it is translated and rotated

  1. 這是立方體的原始 position 及其子圓錐體 object。

在此處輸入圖像描述

  1. 在這里,我已經翻譯了 2 個單位的圓錐體在此處輸入圖像描述

  2. 現在我已經將父立方體相對於 Y 軸旋轉了 25 度在此處輸入圖像描述

  3. 如果我們再次將錐體平移 2 個單位,它就不是原來的 position 在此處輸入圖像描述

在將其轉換回原來的 position 時,我需要將什么公式應用於圓錐體?

如果您在 Unity 中使用 transform.Translate() 方法移動 object 並且您希望保持相對於它的父級,那么一種方法是為 Translate 中的 Space 參數傳入父級的轉換

像這樣:

var parent = transform.parent;

transform.Translate(0, 0, 2, parent); // 2. Over here, I have translated the cone for 2 units
parent.Rotate(0, 25, 0); // 3. Now I have rotated the parent cube around 25 degrees wrt to the Y-axis

transform.Translate(0, 0, -2, parent); // 4. If we translate the cone 2 units back again, it's now in the original position

我在一個新的 Unity 項目中重新創建了您的問題,並使用上面的方法修復了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM