簡體   English   中英

Unity 3D - 如何全局旋轉一個 object 基於秒

[英]Unity 3D - How to gllobaly rotate one object based on second

我在 Unity 中遇到了一個非常大的旋轉問題。 我想要的是:

我有兩個 3D 對象。 只有一個用於玩家操作,第二個 object Transform.rotation 和 Transform.position 依賴於 object 第一個,比例為 1/10。 這意味着如果我首先將 object 從 (0,0,0) 移動到 (10,30,90),那么 obj.2 將從 (0,0,0) 移動到 (1,3,9)。 這很簡單。 但是我在旋轉方面遇到了很大的問題。

我無法對正常變換進行旋轉,因為它基於“本地位置”。 下面我用最簡單的 2D object 情況介紹我的問題:

在此處輸入圖像描述

正如你所看到的,當我旋轉紅色 object +90 度時,第二個 object 旋轉 +9 度,軸相對於世界變得不同。 在 3D 世界中進行更多轉換后,它變得一團糟。 For example after some transformations if I will want to rotate 3D object from me (like using accelerator on motocycle) on first make second object rotating from left to right (because it's based on object axis).

當然,使用 Transform.Rotate 代替 Transform.localRotate (或 Transform.EulerAngles 代替 Transform.localEulerAngles)不是一個解決方案,因為它只意味着對象是子對象(這種情況不是)。

我發現了什么:

使用 Transform.Rotate(Xdegree,Ydegree,Zdegree, Space.World) 是旋轉第二個 object 的解決方案!

我需要的:

Xdegree、Ydegree 和 Zdegree 從第一個(由玩家操作)object。

Transform.EulerAngles 和 Transform.Rotation 不起作用,因為它返回“本地對象”旋轉。

所以...我知道如果 3D obj.2 旋轉為 (0;30;0) 並且我使用 obj2.Rotate(45,0,0) 那么 obj.2 旋轉將為 (~37.76;~39.23;~ 26.56),沒關系。 但我不知道如何轉換另一種方式(從“本地”旋轉 XYZ 到我可以在 Transform.Rotate() 上使用的度數(當然我會在最后將這個值(xyz)除以 10,因為我有 1 /10 移動刻度))

如果您需要一個游戲對象具有 1/10 的旋轉和 position 的另一個,您可以使用類似的東西:

//the player-controlled cube
public Transform t1;
//the 1/10 cube
public Transform t2;

void Update(){
    //set the position of t2 to 1/10 of the position of t1
    t2.position = 0.1f * t1.position;
    //get the axis and angle of t1's rotation
    t1.rotation.ToAngleAxis(out float angle, out Vector3 axis);
    //t2 should be rotated in the same direction (axis), but with 1/10th of the angle
    t2.rotation = Quaternion.AngleAxis(angle * 0.1f, axis);
}

編輯:要允許重置增量旋轉和更改目標,您可以執行以下操作。 注意:當它超過一個完整的圓圈時會出現故障,我不是四元數方面的專家,所以你必須自己弄清楚。

    //the player-controlled cube
    public Transform t1;
    //the 1/10 cube
    public Transform t2;
    private Vector3 t1originalPosition;
    private Quaternion t1originalRotation;
    private Vector3 t2originalPosition;
    private Quaternion t2originalRotation;

    void Start()
    {
        ResetTarget(t1);
    }

    void Update()
    {
        if (t1 != null)
        {
            //set the position of t2 to 1/10 of the position of t1
            t2.position = t2originalPosition + 0.1f * (t1.position - t1originalPosition);

            Quaternion t1Rotation = t1.rotation * Quaternion.Inverse(t1originalRotation);
            //get the axis and angle of t1's rotation
            t1Rotation.ToAngleAxis(out float angle, out Vector3 axis);

            //t2 should be rotated in the same direction (axis), but with 1/10th of the angle
            t2.rotation = Quaternion.AngleAxis(angle * 0.1f, axis) * t2originalRotation;
        }
        
    }

    public void ResetTarget(Transform target = null)
    {
        t2originalPosition = t2.position;
        t2originalRotation = t2.rotation;

        t1 = target;
        t1originalPosition = t1.position;
        t1originalRotation = t1.rotation;
    }

使用四元數而不是歐拉角(xyz 旋轉角)。 並簡單地將一個 object 的全局旋轉值(四元數)給另一個。 要將四元數相加,只需將它們相乘即可。

暫無
暫無

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

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