Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Could someone explain why you have to use the lookvector here?

Asked by 5 years ago
Edited 5 years ago
 local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

mouse.Button1Down:Connect(function()
    local part = Instance.new("Part", workspace)
    local bodyvelocity = Instance.new("BodyVelocity", part)
    bodyvelocity.MaxForce = Vector3.new(math.huge, math.huge,math.huge)
    bodyvelocity.Velocity = CFrame.new(part.Position,mouse.Hit.Position).lookVector  *200
    part.Position = plr.Character.HumanoidRootPart.Position
    print(CFrame.new(part.Position, mouse.Hit.Position).lookVector)
    print(CFrame.new(part.Position, mouse.Hit.Position))
end)


1.Why does using lookvector change the printed message?

2.What lookvector do I get? Is it both of them added or is it just the parts lookvector?

2 answers

Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
5 years ago
Edited 5 years ago

So, in order to understand these shenanigins we will take a deep look into CFrame.

First of all, a lookVector is just a unit vector (unit vector are just vectors with the length of one, or you can say magnitude which is the same as length, and they have a length of one because they are used just to describe a direction, since vectors are lines that have a magnitude (length) and direction, so if their magnitude is 1 we wouldnt care about it so we'll be using this unit vector just to describe a direction), and this unit vector which I said is just to describe a direction is used to describe the direction of where the object is looking, the looking direction, that's why its called a lookvector. Now, I hope that was clear, but I'm sure you are asking "why the heck did we do this in the script?"

print(CFrame.new(part.Position, mouse.Hit.Position).lookVector)
--why are we doing CFrame.new().lookVector

Welp, because the cframe has components

--if you were to
print(CFrame.new():Components()

You can see that it !prints

These are the components of a CFrame, the numbers or in a right way the vectors that construct a cframe. Now if you didn't know, CFrame is position and rotation, and is made up of 4 vectors, the Position vector for the position, and the lookVector, rightVector and upVector, these three being for rotation. An image that might make this simple !An image that might make this simple

You can see the position vector, which actually has a magnitude of 0, because its just a point, but vectors can be replaced with a point. And the three other vectors (btw backVector is the same as lookVector) represeting a rotation by rotating each one of them invidually. And a CFrame is actually a 4x4 Matrix, containing these vectors (a matrix is some sort of thing that holds numbers) like !this.

But this is probarly complicated as crap, so an easier way to break it down is like !this.

Like that, those are the components of a CFrame So yeah, you can get these while you're scripting this way CFrame.ComponentName

local cframe = CFrame.new(part.Position, mouse.Hit.Position)
print(cframe.p) --CFrame.p is refering to the position p is position
print(cframe.rightVector)
print(cframe.upVector)
print(cframe.lookVector)
--and as I said, the rotation vector are unit vectors because they are just for directions, so if you were to do
print(cframe.rightVector.magnitude) --this would print 1 cause the length of these vectors is 1, unit vectors

And that is really all about CFrame, but a simplified version of it, there is way more to embark. And in order to dig deeper you may need some knowledge on Linear Algebra so i would recommend this.

0
Was not expecting an answer like this. Thank you for taking your time and describing this! Thesquid13 301 — 5y
0
No problem! I love doing this starmaq 1290 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

"Could someone explain why you have to use the lookvector here?"

The short answer is: You don't and you shouldn't.

This...

CFrame.new(part.Position,mouse.Hit.Position).lookVector  *200

...is just a way to waste ~4x the memory and CPU time to do what should be this:

200 * (mouse.Hit.Position - part.Position).Unit

Answer this question