|
| 14 Apr 2013 10:39 AM |
I've been trying to understand CFrame.lookVector for a while now, and have posted several questions in the forum about it. I think I got it, until I tried it in ROBLOX Studio. I got a responce saying that it returns the space 2 studs in front of the front surface of the part you use it on, which makes perfect sense, but when I tested it in ROBLOX Studio using Part1 and Part2, I got the following results:
game.Workspace.Part1.CFrame = CFrame.new(game.Workspace.Part2.CFrame.lookVector) - Part1 appears on top of Part2
So then I thought that CFrame.lookVector returns the top surface, since that's what it seemed like. But then, when I flipped the parts around:
game.Workspace.Part2.CFrame = CFrame.new(game.Workspace.Part1.CFrame.lookVector)
I got them both in the same exact position? I think I may know the problem, Im not supposed to call CFrame.new for lookVectors. But I just tried that and it gave an error, expecting a new CFrame, which is what I did in the first place, but that didn't seem to work either. Please tell me how to do this correctly! Thanks.
-- cardgamechampion/AlternativeAcc |
|
|
| Report Abuse |
|
|
| 14 Apr 2013 10:42 AM |
What you're doing there is creating a CFrame based on Part1's lookvector. Unless you change Part1's orientation, you'll get the same result every time.
What you want is a lookVector that points from Part1 to Part2 right? LookVector is a unit vector. It points towards a position. You can get a unit vector more quickly and easily by doing
lookVector = (Part2.Position - Part1.Position).unit
The formula here being u = (b - a).unit |
|
|
| Report Abuse |
|
|
| 14 Apr 2013 10:53 AM |
Ok, I sorta get it. lookVector is different than a CFrame position. OHH I GET IT NOW!!! The reason why my code didn't work is because lookVector is not global it's the other one (forgot the name lol).
I don't get what .unit means at the end. I also don't get what .magnitude means at the end of the subtracting CFrames :P. Can you explain those?
Thanks |
|
|
| Report Abuse |
|
|
| 14 Apr 2013 10:58 AM |
Vector3.unit = normalized copy of the vector - one which has the same direction as the original but a magnitude of 1 Vector3.magnitude = The length of the vector (distance) |
|
|
| Report Abuse |
|
killjoy37
|
  |
| Joined: 27 Aug 2008 |
| Total Posts: 2821 |
|
|
| 14 Apr 2013 10:59 AM |
magnitude is just the distance between the two. .unit creates a vector with the same direction as the provided vector, but only a magnitude of 1. This means it is one stud away from the origin, lookvector though, creates a unit vector relative to the object, so one unit away from the object, in the direction of its front face.
Pretty sure that's right. Someone correct me otherwise |
|
|
| Report Abuse |
|
|
| 14 Apr 2013 11:05 AM |
Thanks so much! Now I get what .magnitude means. So like if you have 2 parts, 1 5 studs away from the other, and you have the position of 1 part minus the position of the other part in parenthesis .magnitude, it returns the distance between the two, which is 5, right?
I still don't get what .unit means, but that's ok. I learned something :D. |
|
|
| Report Abuse |
|
|
| 14 Apr 2013 11:20 AM |
Yep, that's right.
Before you understand unit, you have to understand how a unit vector works. Say we have vector A at point (0,-5,3). This means A's points are (X=0, Y=-5, Z=3). X, Y, and Z each represent a spatial plane on which the part can exist. X is left when negative, right when position. Y is bottom/down when negative, top/up when position. Z is back when negative, front when positive. So, A is in the middle-bottom-front from the view of (0,0,0). That's what a unit vector gives us. The unit vector just tells what direction a point is in relative to (0,0,0). So, A's unit vector would be (0, -0.857492924, 0.51449573) because that's the direction it's in.
Unit is the same thing as a lookVector. Imagine we have Vector A at the points (2,0,0). A.unit is (2,0,0) because A is facing right. If B is at (2,0,-2), then B would be in the Right-Back of (0,0,0). But, we can do C=(B-A). This gives us the space from A to B, which is (0,0,-2). If we take C.unit, which is (0,0,-1), we get the direction that we need to go to get from A to B.
Magnitude is very useful if you want to multiply things. Magnitude is the number of studs between a point and (0,0,0). So, if we take A and B from the previous paragraph, A.magnitude is 2, because A is 2 steps away from (0,0,0). It gets more complicated when you add extra numbers, though, which is why we have the magnitude property. B.magnitude is 2.8284271247462, and that would be pretty annoying to have to do by hand.
If we use these two things together, we can do some pretty neat stuff. For example, we can keep A at the same direction from (0,0,0), but move it to the same distance as B.
new_A = A.unit * B.magnitude
Also, using magnitude, I can make this red part rotate around (0,0,0) by moving this blue part further away from (0,0,0). Try running this in the command bar:
A = Instance.new("Part",workspace) A.Color = Color3.new(1,0,0) A.Name="A" B = Instance.new("Part", workspace) B.Color = Color3.new(0,0,1) B.Name="B" while wait() do A.CFrame = CFrame.new(math.sin(B.Position.magnitude / 10 * math.pi)*10, 1, math.cos(B.Position.magnitude / 10 * math.pi)*10) end |
|
|
| Report Abuse |
|
|
| 14 Apr 2013 11:30 AM |
| Thanks. I'll have to read this over and over again more carefully, but this looks very good, and I think if I read it over a few times I'll get unit too. Thanks so much! |
|
|
| Report Abuse |
|