LookVector and how to use it

Due to roblox’s documentation being the most spaghetti text there is, I can’t really make sense of :

cf = cf + cf.LookVector * n 

If someone knows how to use look vector and can explain it like a normal person, that would be so greatly appreciated. I’m simply trying to put a part 3 studs in front of my character’s HumanoidRootPart.

4 Likes

The LookVector is a unit vector (a vector of length 1) pointing in the direction that the CFrame is facing. Multiplying this LookVector by a scalar n will extend the vector out in the same direction but n times longer. So, if n is 3, the vector will be 3 times as long as the LookVector . This operation cf.LookVector * n is creating a vector that points n units straight ahead from the current orientation of the CFrame.

4 Likes

honestly, i’m not too sure myself, but what i do know is that if you wanna get some distance in front of something, just do

local CFRAME = HumanoidRootPart.CFrame -- insert whatever CFrame you want here
local DISTANCE = 3 -- as usual, this is distance in studs

local result = CFRAME * CFrame.new(CFRAME.LookVector * DISTANCE) -- this here will be DISTANCE studs in front of CFRAME

ye, if you want a full in depth explanation, we will have to consult the rotation matrixes
my smooth brain cannot comprehend them, so for normal uses, this code block above works ig

3 Likes

Basically you would do

HumanoidRootPart.Position += HumanoidRootPart.CFrame.LookVector * 3

How this works is that the LookVector is the direction the Front face of a part (CFrame?) is facing. Since it is a UnitVector, if you add it to another Vector3 / position the will position move one stud in that direction. You can multiply it to amplify this effect, leaving us with the code above. Which moves the HRP 3 studs forward in the direction the front of it is facing.

2 Likes

Thank you to all 3 of you, @au_tk, @NGC4637, @RipGobtain, I wish i could give all 3 of you the Solution tag since all of you helped me understand this and ultimately get it working.

1 Like

No worries! I don’t really care about having the solution checked on me, but I do care about helping others and making sure they can solve their errors.

2 Likes

Thank you so much, you guys are a blessing that roblox doesn’t deserve LOL

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.