generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: CFrame.lookVector help?

Previous Thread :: Next Thread 
AlternativeAcc is not online. AlternativeAcc
Joined: 29 Mar 2012
Total Posts: 1337
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
ArceusInator is not online. ArceusInator
Joined: 10 Oct 2009
Total Posts: 30553
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
AlternativeAcc is not online. AlternativeAcc
Joined: 29 Mar 2012
Total Posts: 1337
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
Unsubstantial is not online. Unsubstantial
Joined: 11 Sep 2012
Total Posts: 872
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 is not online. 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
AlternativeAcc is not online. AlternativeAcc
Joined: 29 Mar 2012
Total Posts: 1337
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
ArceusInator is not online. ArceusInator
Joined: 10 Oct 2009
Total Posts: 30553
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
AlternativeAcc is not online. AlternativeAcc
Joined: 29 Mar 2012
Total Posts: 1337
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image