- Home /
How to rotate an array of vectors over a normal vector
hi guys! hope that you're fine!
im making a voxel engine, and im looking for a way to do vector rotation i've use Vector3.Reflect() and Vector3.project() over all vectors in array,
to show you how its done i leave a little code below,
however, its no work cuz im storing all block vertex data in that array, so when i run this code them make all vectors project over 4 points, i dont know how to use it, i want to project or rotate all vertex index to do for example, vertex = (0,1,1) --->(1,1,0).
what i want to get is a system like minecraft switch block face pending on player direction
thank you anyway!
the code:
public class Test : MonoBehaviour
{
public Vector3[] verts = new Vector3[]{
new Vector3 (-0.5f, 0.5f, 0.5f),
new Vector3 (0.5f, 0.5f, 0.5f),
new Vector3 (0.5f, 0.5f, -0.5f),
new Vector3 (-0.5f, 0.5f, -0.5f),
new Vector3 (0.5f, -0.5f, 0.5f),
new Vector3 (-0.5f, -0.5f, 0.5f)
};
public Vector3 normal = new Vector3 (0, 1, 0);
public Vector3[] to = new Vector3[4];
void Update ()
{
if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit)) {
Debug.Log (hit.normal);
normal = hit.normal;
}
to = new Vector3[verts.Length];
for (int i = 0; i < verts.Length; i++) {
to [i] = Vector3.Reflect (verts [i], normal);
}
}
RaycastHit hit;
void OnDrawGizmos ()
{
Gizmos.color = Color.green;
var last = to [0];
for (int i = 0; i < to.Length; i++) {
if (i > 3) {
Gizmos.color = Color.blue;
}
Gizmos.DrawCube (to [i], Vector3.one * 0.1f);
if (last != to [i]) {
Gizmos.DrawLine (last, to [i]);
last = to [i];
}
}
}
}
beside, i guess i have a more optimized and useful way to do what i want without mesh vertex data modification, i dont try it, but i guess i can set the face uv iterating and casting the RaycastHit.normal with a switch() iteration, i dont know but if it work i leave the solution below, ;)
Answer by Monstermash28425R1 · Jan 29, 2019 at 04:03 PM
figure out my problem
var m = Matrix4x4.TRS(Vector3.zero,Quaternion.LookRotation(normal),Vector3.one);
vector = m.multiplyPoint3x4(vector);
and vector will "rotate" it values
"normal" is the vector were we want to rotate our vector