- Home /
Need help to get 3D rotation from 2 points in space
This might have been answered already, but I've read many topics about rotations this afternoon and didn't manage to make it work (I'm terrible with 3D transformations...).
I have:
2 points in space: pointA and pointB.
a cube which I want to be connected to both points.
So far, I managed to place my cube right inbetween my points, and scale it on the X axis by the distance between these 2 points. Now the last thing I need is to manage to rotate my "cube" so that both ends of the local X axis end up on my 3D points.
I've tried using the lookout(), using the eulerAngles and many other different ways, but I just couldn't get anything because I fail transferring the math found on some related answers to my specific need.
Can anyone help me please?
Answer by robertbu · Feb 25, 2013 at 03:51 PM
v3Pos1 and v3Pos2 are the two point:
transform.rotation = Quaternion.FromToRotation(Vector3.up, v3Pos2 - v3Pos1);
Thanks man! I was disappointed at first, because your solution wasn't working per se. But then, I used this little head of $$anonymous$$e, and managed to understand that in my specific case I need to replace vector3.up by vector3.right as the direction I want to align is the x-axis. Now it works perfectly!