i want to add the transform of a child into a transform array, and change the transform position that is in the array without affecting the object transform
for (int i = 0; i < transform.childCount; i++)
{
newPositionCellFall.Insert(i, transform.GetChild(i).transform);
newPositionCellFall[i].position = transform.GetChild(i).position - new Vector3(0f, Speed, 0f);
}
Transform is a Unity component, so you can’t simply make a copy of the Transform without making a new object in your scene. So you’ll have to copy the data itself out of the transform. If the position is the only part of the Transform’s data you need, you can make a Vector3[ ]; if you need more information than that, you’ll want to create your own class containing all of the fields you need.
So to copy data into your own Vector3 array, it’d look something like:
Vector3[] cellPositions = new Vector3[transform.childCount];
for (int i = 0; i <transform.childCount; i++) {
cellPositions[i] = transform.GetChild(i).position;
}
And then you have positions in an array that you can change however you need.
It is kind of annoying that Transform does not expose the actual core array of 16 floats representing the true local matrix. It exposes the .localToWorld matrix data but that has already baked in the product of all parent/ancestor matrices.
It doesn’t store the local as a matrix. It stores local position, rotation and scale which you can retrieve. You can create a matrix of this with this.
Thanks, MelvMay. I always assumed it started from the matrix, but seeing as how you don’t support skews and other fun things a matrix can do, I guess not.
Exactly ^^. Depending on the task it’s sometimes even faster to just use the quaternion if you need the rotation. Though you can always create the local matrix by using Matrix4x4.TRS. You could even create an extension method to retrieve the local matrix:
public static class TransformExt
{
public static Matrix4x4 GetLocalMatrix(this Transform aTrans)
{
return Matrix4x4.TRS(aTrans.localPosition, aTrans.localRotation, aTrans.localScale);
}
}