2
votes

as the title suggests I am trying to rotate a plane around a point but the result is not what I expected.

enter image description here

With my editor I create a main mesh (which is the one with the red outline). Then using the four vector3 represented by the white spheres I create a second mesh. Now I need to rotate this mesh on the point where a gray sphere is located. With

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

I find the center of the two Vector3. Using a button I would like to rotate the mesh one degree at a time. I thought I could do it using

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

but any [Vector3] I use the mesh rotates to the point defined by myCenter but moving to the right or left. I can't find the correct value for [Vector3]. Is it possible that [Vector3] needs to be changed every time the mesh moves one degree? Can you help me?

2
Your Vector3 should be (Sphere1.position - Sphere2.position).normalized, so you've already worked it out when you found myCenterImmersive
Thanks, that's right. This is the correct answer for my case. You should post it as an answer so that I can accept it.valter

2 Answers

1
votes

I had a pretty similar problems (I wanted to flip a level which contains cubes but it's makes random stuffs).

So, I create my custom editor script which make a Parent object which center pivot between childs objects (the center you need):

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");
    }

    }
}

After that, I used DOTween in order to make this flip http://dotween.demigiant.com/

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

m_Level is parent (your center).

In your case, you should use CenterPivotObject ove rand object called "__PARENT" which contains your two Vector3, and use Editor Script.

After that, change Vector3(180f, 0f, 0f) in order to achive your movement desire.

1
votes

Your Vector3 should be (Sphere1.position - Sphere2.position).normalized, so you've already worked it out when you found myCentre