Question by
minnes · Jul 11, 2018 at 09:32 AM ·
rotationtranslationmatrix4x4.trs
Rotation and translation between objects with TRS matrix
I have two objects in the scene and I want that one goes in the same position of the other with the same rotation. With this I also want to store the translation and rotation that the object has done.
My idea is to create a TRS matrix that stores the translation and rotation, so that I can use it easily on the other part of the code, but when I try to move the object with this the result is wrong. Am I missing something?
public Matrix4x4 trs;
public void SetTRSValues(GameObject other)
{
Vector3 translation = other.transform.position - transform.position;
Quaternion eulerAngles = Quaternion.FromToRotation(transform.forward, other.transform.forward);
trs.SetTRS(translation, eulerAngles, Vector3.one);
transform.position = trs.MultiplyPoint3x4(transform.position);
}
If I use this method the translation and rotation work fine, but I would like to use the TRS matrix, if possible, and I would like to learn how to use the TRS matrix correctly
public void MoveObject(GameObject other)
{
Vector3 translation = other.transform.position - transform.position;
Quaternion offsetRot = Quaternion.FromToRotation(transform.forward, other.transform.forward);
Vector3 eulerAngles = offsetRot.eulerAngles;
transform.Rotate(eulerAngles);
transform.position += translation;
}
Thank you for the help
Comment