Quick question about .Unit

Hello, so, lol! Imagine scripting with something that you don’t know…

while true do
local ray = Ray.new(script.Parent.CFrame.Position, script.Parent.CFrame.LookVector.Unit * 300)
local hit,pos = workspace:FindPartOnRayWithWhitelist(ray, game.Workspace:GetChildren())
if hit then
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		print(plr.Name)
		local Speed = hit.Velocity.Magnitude/2
		if Speed >= 1 then
			warn(plr.Name.." is speeding :(")
		end
	end

end
wait()
end

That’s literally what I did… so what’s Unit? it says ‘normalized direction…’ what’s this? Like I don’t get it…

Can you give me really simple example please :slight_smile: thanks

5 Likes

In simplest terms, a unit is a normalized vector, which has a length of 1.

I guess a good way of visualizing the difference would be with the following code:

local direction1 = somePart.CFrame.lookVector * 100
local direction2 = somePart.CFrame.lookVector.Unit * 100

If you set a part’s velocity to each variable above, you should notice a huge difference between the two. The second one should be more stable, the reason for this is that we basically reduce the length of the lookVector to 1, and then multiply that by the length that we want, contrary to just multiplying the already large vector by 100.

I hope this helps :slight_smile:

13 Likes

LookVector, as well as RightVector and UpVector, is a normalized vector already, so there actually won’t be a difference. Although, if you print the magnitude of lookVector, you might not get 1, due to floating point imprecision, but if you do normalize that vector again and print its magnitude, it would be a bit more accurate

3 Likes

Does the look vector always remain 1? It’s basically the area the part is facing forward right?

It has the same direction as a given vector, with each axis scaled from 0-1.

1 Like