Answered by
7 years ago Edited 7 years ago
Let's say one part (let's call it "Brick") is at the point Vector3.new(0, 0, 0) and another (let's call it "Brick1" is at Vector3.new(5, 7, 6). To calculate the angle, we will be using math.asin, which is the inverse of sine. If you haven't taken trigonometry, inverse sine is used to find an angle in a triangle based on the fraction of the length of the opposite side of the angle divided by the hypotenuse. In this case, the hypotenuse is the magnitude of the two vectors (magnitude is the distance between two points). To calculate the magnitude, one would write:
1 | local Magnitude = (Brick.Position - Brick 1. Position).Magnitude |
Then we need to find the opposite side of the angle, which is the height difference of the part. To do this, we will be using the Y values of the two positions. distance formula and math.sqrt, except using only two points. So, our code would then be this:
1 | local Magnitude = (Brick.Position - Brick 1. Position).Magnitude |
3 | local Height = math.sqrt((Brick.Position.Y - Brick 1. Position.Y)^ 2 ) |
We've just pretty much created two sides of a triangle, which is all we need for inverse sine. Our code would then be:
1 | local Magnitude = (Brick.Position - Brick 1. Position).Magnitude |
3 | local Height = math.sqrt(Brick.Position.Y - Brick 1. Position.Y) |
5 | local Sine = Height / Magnitude |
7 | local Angle = math.asin(Sine) |
9 | local AngleInDegrees = math.deg(Angle) |
If you have any problems or need any clarification, feel free to comment.