Vector3.Unit can someone explain this

I’ve been really getting into scripting this year
and I got a question
I have recently been learning how to script guns and other projectile stuff I have been using raycasting
I have successfully scripted guns before but I want a full understanding on how things work
now to the question
what is a Vector3.Unit and what does it do can someone explain in details or a good video on how to learn this
I read other forums post that didn’t really make sense to me
165759fe8b4c47bcd482163ddb493d29
and also the API that Roblox gives out as help(Magnitude of 1??)


any help is appreciated

26 Likes

I’m guessing that a unit is just another vector with a distance of 1 between the original vector. Magnitude (regarding Roblox, magnitude can mean other things) is a fancy word for distance, because the length of a vector between two points is just distance.

Edit: It’s not another vector. It is simply one vector with a magnitude of 1. In short, it’s a Vector with a distance of 1.

A much better, not rushed explanation of Vectors:

7 Likes

A Unit Vector is a Vector who’s values are based on 1. This is basically only used for directional vectors as far as I know.

The main use of this for me personally, is to condense the values down, so that you have a directional Vector that can be extended out a desired distance, while still maintaining the direction aspect.

For example:

BasePartPosition = workspace.BasePart.Position
LookAtPartPosition = workspace.LookAtPart.Position

Direction = LookAtPartPosition - BasePartPosition --right now this Vector3 direction does not have a Magnitude of 1

UnitDirection = Direction.Unit --now we can multiply it by a certain number, to extend it out that many studs in a certain direction.

ExtendedDirection = UnitDirection * 20 -- this is a direction that goes 20 studs towards the LookAtPart, from the Position of the BasePart

Without the .Unit, the Vector is not normalized, so we can’t calculate the exact distance we want the vector to extend in said direction.

A use case would be allowing you to shoot out a Ray towards the Mouse.Hit a specific distance:


local vector = (mouse.Hit.p - Tool.PrimaryPart.Position).Unit

Ray1 = Ray.new(Tool.PrimaryPart.Position, vector * 20)

Hope this makes sense!

37 Likes

I think I finally get it after month’s Vector3.Unit is basically is used to extend the size while holding the rotation you are already pointing at I thought it will be more complicated but I was wrong
Thank you to yall both @ianscript @ExcessEnergy
Thank you for replying you really helped me.

8 Likes

.Unit Basically makes a vector pointed from one position to other which make it very useful