Changing mesh of GameObject doesn't changes the position of its transform

Hi,

I’m trying to transform my GameObject via a 4x4Matrix which works already. But after the mesh is transformed the position stays the same even, when it should change.

Example:
I try to scale the GameObject by the factor of two in direction of the x-axis. The position is: (2, 0, 0). So after transforming the vertices with a 4x4 Matrix like:
2 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

the position should be (4, 0, 0). But it still is (2, 0, 0).

This is only an example. I know there are methods provided by Unity to scale objects but in my case I will get 4x4Matrices which will combine several different transformations (including shearing) so I have to apply them directly on the objects/vertices.

Here is my code:

var mf : MeshFilter = gameObject.GetComponent.<MeshFilter>();
    var vert : Vector3[] = mf.mesh.vertices;
    for (var i = 0; i < vert.Length; i++)
    {
        vert[i] = matrix.MultiplyPoint(vert[i]);
    }
   
    mf.mesh.vertices = vert;
    mf.mesh.RecalculateBounds();

Thanks for your help!

The scale is local, not global. Scaling the object will never change its position.

You are right, but only when I use the scale method from unity. But I use my own 4x4 Matrix. And there the position in game is changing but the values of the transform position does not. To set that straight.

As far as I know: When I multiply a 4x4 Scale-Matrix directly on a vertice from an object which is not in the origin the position of the object changes. Seems logical as the vertice is a Vector and if you multiply it with a matrix it has to change values.

Found a solution:

function transform (gameObject : GameObject, matrix : Matrix4x4)
{
   
   
    var mf : MeshFilter = gameObject.GetComponent.<MeshFilter>();
    //Get the starting position of gameObject
    var pos = gameObject.transform.position;
    //Calculate the end position of gameObject
    pos = matrix.MultiplyPoint3x4(pos);
    //Move GameObject in origin
    gameObject.transform.position = Vector3(0,0,0);
    //Do the manipulation
    var vert : Vector3[] = mf.mesh.vertices;
    for (var i = 0; i < vert.Length; i++)
    {
        vert[i] = matrix.MultiplyPoint3x4(vert[i]);
    }
   
    mf.mesh.vertices = vert;
    mf.mesh.RecalculateBounds();
    //Move gameObject back to end position
    gameObject.transform.position = pos;
}