Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

get matrix of local transform

Discussion in 'Scripting' started by ghiboz, May 19, 2015.

  1. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    hi all!
    I wish obtain the matrix generated with transform.localPosition and transform.localRotation..
    how can I do? if I use the transform.worldToLocalMatrix is not correct, 'cause took the parent global position, and I don't want it!

    thanks
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    transform.localToWorldMatrix.inverse perhaps?
     
  3. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    nope... :(
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Perhaps you could describe in more detail what you need?
     
  5. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    I have a mesh into my gameobject, and I need to cycle to the vertex, and I need the position relative to the parent (not to the world), 'cause this mesh may be scaled, rotated or translated

    thanks @blizzy
     
  6. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    MagyarPeter, oleg_v, Bunny83 and 2 others like this.
  7. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    Bunny83 and col000r like this.
  8. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    Another way to "remove" one transformation matrix from another is to multiply its inverse with your original matrix.

    local_matrix = transform.parent.localToWorldMatrix.inverse * transform.localToWorldMatrix;

    I've found the functions for creating new matrices from position, rotation and scaling to be somewhat unreliable/work somewhat unexpectedly for flipped objects. I'm also a SketchUp plugin developer so I've lived a few years now in Matrix Land :p .
     
    oleg_v, voxelltech, Singtaa and 3 others like this.
  9. voxelltech

    voxelltech

    Joined:
    Oct 8, 2019
    Posts:
    44
    This is a life saver! Thanks!
     
  10. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    I have never seen this. Note that we talk specifically about using the localPosition, localRotation and localScale. You can not use the position, rotation and lossyScale because the scale will be the actual issue here. Using those 3 would somewhat work for transformations that do not contain any shearing. However if you need the localToWorld matrix, you would just use the localToWorld property anyways ^^.

    The reason why using the lossyScale does not work is that the TRS method can only construct a 3x4 matrix from the given information. If you have a shear going on you usually need the 4th row of the matrix. This can not be represented by a position, rotation and scale alone.

    However for a local matrix, this should always work:

    Code (CSharp):
    1. var local_matrix = Matrix4x4.TRS(t.localPosition, t.localRotation, t.localScale);
    Of course
    t
    is the Transform you're interested in.

    For everyone reading this, the local matrix is the matrix that brings you from object / local space into parent space. Of course the approach that eneroth3 took is a general way to convert from one space to another. by meeting at a common space. Note that there's no reason to do
    localToWorldMatrix.inverse
    since the Transform component provides the inverse already as
    worldToLocalMatrix
    .
     
    oleg_v likes this.