I need help understanding “.Unit”

I am aware that I have already created a similar topic like this before, however I am still not clear about what .Unit is and how it works and it’s purpose in this script:

Any links to Roblox Developer pages explaining .Unit are also appreciated.

2 Likes

It’s more of a math problem than a scripting problem, but this post should do with a neat little diagram.

4 Likes

Oh! So basically it just gives you the direction? That would also explain why in the code he multiplies it with the Agro Distance, correct?

If you want a solid understanding of the math, I’d recommend Khan Academy. It’s not 100% necessary though and there’s a lot of details that you don’t really need to make games.

1 Like

Not sure what code you’re referring to. No “AgroDistance” in the code you posted.

In the code you posted, a unit vector is multiplied by 500 to get a raycast of exactly length 500. This is useful if you e.g. want to limit the range to 500 studs.

1 Like

Oh yea, just replace Agro Distance with 500, is my theory correct?

Sure, sounds like it30charcters

1 Like

.Unit is a normalized vector. What the line does is subtract 2 positions (Vectors) and get the .Unit of it multiplied by 500.

Take this for an example:

-- assuming the part's position is 0, 5, 0 
print(part.Position.Unit) 

-->> 0, 1, 0  { this is because .Unit vector always has a length of 1 `}

Note that using .Unit doesn’t reduce the position to 0, 1, 0, it always has a length of 1.

Another example:

-- assuming part's position is at 0, 10, 0

print(part.Position.Unit * 5)

-->> 5

We multiply .Unit which has a length of 1 by 5. It is also worth mentioning that using .Unit on positions is a lot stable than just using positions because it has a length of 1.

1 Like

So basically .Unit gives us the direction with a length of 1 which we then multiply by 500 (length)?

1 Like

Yes! Precisely!

By doing:

(torso.Position - turret.Position)

You firstly find the vector between two points (your torso and the turret) of a certain direction (pointing from the turret to the torso) and length.

Then by getting the .Unit property of that vector, you find the directional component of that vector.

By multiplying by 500, you then get a vector that is 500 in length and its direction is pointing from the turret to the torso.

So in essence, that entire line of code in the original picture that you posted creates a new ray that starts from the turret, points in the direction of the torso and has a length of 500.

Update:

Also! To perhaps clarify a little bit on a previous post!

-- assuming part's position is at 0, 10, 0

print(part.Position.Unit * 5)

-->>  0 , 5 , 0 and NOT just 5

Printing a vector does not give you the magnitude just like that. When we do part.Position.Unit we are getting a vector that represents the direction of the position of the part from the centre of the map (the origin). So when we multiply that vector by 5, we simply “extend” the vector to 5 times its original length. Which in this case, since the vector is a unit vector, it does now have a length of 5 but we must still remember the directional component of the vector!

5 Likes