2017-01-05 51 views
0

我想旋轉一個對象,就像「Transform.RotateAround」一樣。 ,而不是增加每個呼叫的角度。 它應該只是設置角度,以確切的角度,就像當使用「transform.localEulerAngles」等效於「Transform.RotateAround」但是不增量?精確的角度

我一直在尋找年齡現在,但無法找到一個好的,乾淨和簡單的解決方案。

我已經發現了這一點: http://answers.unity3d.com/questions/537155/is-there-a-method-to-set-the-exact-rotation-instea.html

它表明每個「Transform.RotateAround」調用之前重置旋轉,但不知何故,此不爲我和它的工作(最好的「黑客」之一)不是一個很好的解決方案。

+0

你到底想達到什麼目的?你有沒有考慮過在對象中居中放置一個空的GameObject,並在對象中旋轉偏移量?這將抽象出涉及的複雜計算。我覺得我已經解決了過去這個問題的變體,像[這裏](http://stackoverflow.com/questions/40897629/)和[這裏](http://stackoverflow.com/questions/40901164 /)。所有這些都歸結爲一個空的容器對象。 – Serlite

+0

事情是我想旋轉同一物體在不同位置的許多不同點。一次。比製造空容器和抵消它們更動態。 – swisswiss

+0

如果要圍繞另一個點旋轉對象,只需將容器對象重新定位到該點的居中位置,翻譯包含的對象(以調整旋轉半徑),然後相應地旋轉它。或者是否有一個條件使得這種方法無法使用? (這將是相關的徹底解釋在這個問題上,因爲它看起來並沒有顯示出與我所看到的變體大不相同。) – Serlite

回答

1

編輯:修訂後的答案因誤會

旋轉一個對象的周圍另一個對象B包括兩個步驟:

  1. A的
  2. 更新localRoation(更新位置繼續尋找在B處與同一面)。

這可以用下面的示例代碼,其中變換屬於腳本在對象B的左右旋轉rotationAxis實現

using UnityEngine; 

public class rot : MonoBehaviour { 
    public GameObject reference; 
    public float angle; 
    public Vector3 rotationAxis; 
    Vector3 direction; 
    void Start() { 
     // determine direction only once, to avoid infinite rotation. 
     direction = transform.position - reference.transform.position; 
    } 
    void Update() 
    { 
     Quaternion rot = Quaternion.AngleAxis(angle, rotationAxis); 
     transform.position = reference.transform.position + rot * direction; 
     transform.localRotation = rot; 
    } 
} 
+0

這不能工作。因爲它只設置對象的旋轉。圍繞某物旋轉某些物體也必須改變物體的位置。 – swisswiss

+0

嗯,也許我不明白你的評論,但這正是我使用localRotation的原因。它圍繞提供的軸轉變整個變換(隱式地通過變換的樞軸點(0,0,0))。 編輯:沒關係,現在我明白了。我首先誤解了你的問題。對不起 – PlantProgrammer

+0

這正是我所期待的。和4行代碼。 頭腦清醒。哈哈 – swisswiss