07-05-2021, 04:26 PM
(This post was last modified: 07-05-2021, 04:27 PM by josemendez.)
(07-05-2021, 04:12 PM)Spectra_7 Wrote: Hello.
First of all thanks for that coding tip.
I know I am troubling you a lot, but please I really need help regarding Matrix4x4 you mentioned.
Code:private void HandleGlitters()
{
if (glittersList.Count <= 0)
return;
for (int i = 0; i < glittersList.Count; i++)
{
Matrix4x4 particlesMatrix = Matrix4x4.TRS(slimeActor.GetParticlePosition(glittersList[i].particleIndex), slimeActor.GetParticleOrientation(glittersList[i].particleIndex), Vector3.one);
glittersList[i].transform.position = MatrixUtils.ExtractPosition(particlesMatrix) - glittersList[i].particleAttachOffset;
glittersList[i].transform.rotation = MatrixUtils.ExtractRotation(particlesMatrix);
}
}
Is this code correct? Because those small spheres are still floating in air.
This is the MatrixUtils class:
Code:using UnityEngine;
public static class MatrixUtils
{
public static Quaternion ExtractRotation(this Matrix4x4 matrix)
{
Vector3 forward;
forward.x = matrix.m02;
forward.y = matrix.m12;
forward.z = matrix.m22;
Vector3 upwards;
upwards.x = matrix.m01;
upwards.y = matrix.m11;
upwards.z = matrix.m21;
return Quaternion.LookRotation(forward, upwards);
}
public static Vector3 ExtractPosition(this Matrix4x4 matrix)
{
Vector3 position;
position.x = matrix.m03;
position.y = matrix.m13;
position.z = matrix.m23;
return position;
}
public static Vector3 ExtractScale(this Matrix4x4 matrix)
{
Vector3 scale;
scale.x = new Vector4(matrix.m00, matrix.m10, matrix.m20, matrix.m30).magnitude;
scale.y = new Vector4(matrix.m01, matrix.m11, matrix.m21, matrix.m31).magnitude;
scale.z = new Vector4(matrix.m02, matrix.m12, matrix.m22, matrix.m32).magnitude;
return scale;
}
}
This script does exactly the same as your previous one, but in a more convoluted way: you’re storing the position in a matrix, then immediately extracting and applying it. So the result is the same as before.
Also, you’re applying the particle rotation to your sphere directly, which will make no differente since a sphere rotating around its center looks as if was not rotating at all.
You need to express the position of your sphere in the particle’s local space. Read about local/world space and basis vectors/matrices, this is a fundamental part of making games that you will use very often.
On monday I will write a small sample script for you. cheers!