Reputation: 803
I have a line between two points (Vector3) V1 - V2
I add an object on this line but initially it is always horizontal as in the picture on the left
It is a mesh object - IMPORTANT
How do I rotate this object to be along this line as in the picture on the right?
IMPORTANT to rotate every point in this mesh (not an object)
For further work, I need the exact locations of all the vertices
I try something like this but it's not the way to go:
Vector3 v = V2 - V1;
Quaternion rotation = Quaternion.LookRotation(v, new Vector3(1, 1, 0));
rotation *= Quaternion.Euler(0, 90, 0);
// I rotate all the points in the mesh
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = RotatePointAroundAxis(vertices[i], pos, Vector3.up, rotation.eulerAngles.y);
}
public Vector3 RotatePointAroundAxis(Vector3 pointToRotate, Vector3 centerAxis, Vector3 axis, float angle)
{
Vector3 point = Quaternion.AngleAxis(angle, axis) * (pointToRotate - centerAxis);
Vector3 result = centerAxis + point;
return result;
}
Upvotes: 0
Views: 555
Reputation: 90580
It is way easier simply using
Vector3 v = V2 - V1;
yourObject.transform.right = v;
or if you want to go for one of the other axes
yourObject.transform.forward = v;
or accordingly
yourObject.transform.up = v;
depending on your use case and desired result
After your Update I see you want/need it rather on mesh basis.
Here I would use a Matrix like e.g.
var deltaRotation = Quaternion.FromToRotation(Vector3.right, v);
var matrix = Matrix4x4.TRS(Vector3.zero, deltaRotation, Vector3.one);
var verts = mesh.verts;
for(var i = 0; i< verts.Length; i++)
{
verts[i] = matrix.MultiplyPoint3x4(verts[i]);
}
mesh.verts = verts;
again if needed change the Vector3.right
to Vector3.forward
or Vector3.up
etc accordingly.
Upvotes: 1