簡體   English   中英

Unity:圍繞兩個Vector3的中心旋轉網格

[英]Unity: Rotate a mesh around the center of two Vector3

如標題所示,我正在嘗試圍繞某個點旋轉平面,但是結果卻不是我所期望的。

在此處輸入圖片說明

用我的編輯器創建一個主網格(帶有紅色輪廓的主網格)。 然后使用白色球體代表的四個vector3創建第二個網格。 現在,我需要在灰色球體的位置上旋轉該網格。

Vector3 myCenter = Vector3.Lerp(point1, point2, 0.5f)

我找到兩個Vector3的中心。 我想使用按鈕將網格一次旋轉一個角度。 我以為我可以用

myMesh.transform.RotateAround(myCenter, [Vector3], 1f)

但是我使用的任何[Vector3]網格都旋轉到myCenter定義的點,但向右或向左移動。 我找不到[Vector3]的正確值。 網格每次移動1度,是否可能需要更改[Vector3]? 你能幫助我嗎?

我遇到了一個非常類似的問題(我想翻轉一個包含多維數據集的關卡,但這是隨機的東西)。

因此,我創建了自定義編輯器腳本,該腳本創建了一個Parent對象,該對象的中心在childs對象之間(您需要的中心):

using UnityEngine;
using UnityEditor;

public class CenterPivotEditor : MonoBehaviour
{
    [MenuItem("Tools/CenterPivot")]
    private static void CenterPivot()
    {
        try
        {
        GameObject __PARENT = GameObject.Find("__PARENT");
        Vector3 centroid = Vector3.zero;
        Transform[] childs = __PARENT.transform.GetComponentsInChildren<Transform>();
        foreach (Transform go in childs)
        {
            Debug.Log(go.name);
            centroid += go.position;
        }
        centroid /= (__PARENT.transform.childCount);
        GameObject centerPivotObject = new GameObject();
        centerPivotObject.name = "CenterPivotObject";            
        centerPivotObject.transform.position = centroid;

        foreach (Transform go in childs)
        {
            go.parent = centerPivotObject.transform;
        }
        DestroyImmediate(__PARENT.gameObject);

    } catch(System.NullReferenceException notfound)
    {
        Debug.Log("__PARENT not found. Can't center pivot. Please rename GameObject to __PARENT in order to CenterPivot");
    }

    }
}

之后,我使用DOTween來進行此翻轉http://dotween.demigiant.com/

m_Level.transform.DORotateQuaternion(Quaternion.Euler(new Vector3(180f, 0f, 0f)), durationFlip);

m_Level是父級(您的中心)。

在您的情況下,應使用包含兩個Vector3的CenterPivotObject rand對象“ __PARENT”,並使用編輯器腳本。

之后,更改Vector3(180f,0f,0f)以實現您的運動願望。

您的Vector3應該是(Sphere1.position - Sphere2.position).normalized ,因此,當您找到myCentre時,您已經對其進行了myCentre

暫無
暫無

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

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