Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rotating procedural generated mesh

Discussion in 'Scripting' started by unity_FTxS5cv18wXsTA, Mar 16, 2018.

  1. unity_FTxS5cv18wXsTA

    unity_FTxS5cv18wXsTA

    Joined:
    Mar 1, 2018
    Posts:
    1
    Hello there!

    I have a script which expands a mesh at runtime with additional objects. Everytime such an object is created and added to the mesh, I move an empty gameobject as pivot to the new center of the mesh and rotate it.

    The problem is, that if I rotate the pivot by code, the mesh is not rotated around the center so it changes its position.
    If I look in the scene view while running the application and setting the pivot to the center, the pivot moves to the expected location. If I then apply the rotation to the object from code, the pivot is not in the center anymore.

    Can anybody tell me what I' misunderstanding here?
    In the editor the pivot mode is turned on.

    Code (CSharp):
    1.  public void SetPivot(HexCoordinates coordinates)
    2.     {
    3.         transform.parent = null;
    4.         pivot.transform.localPosition = HexCoordinates.FromHexCoordinates(coordinates);
    5.         transform.parent = pivot.transform;
    6.     }
    7.  
    8.     public void Rotate(Vector3 eulerAngles)
    9.     {
    10.         transform.parent = pivot.transform;
    11.         pivot.transform.localRotation = Quaternion.Euler(eulerAngles);
    12.         transform.parent = null;
    13.     }