繁体   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