What is mouse.unitray

Can someone explain the use of mouse.UnitRay I heard it is used for weapons but have no idea what it is actually doing any help thanks.

1 Like

It’s a ray to the mouse’s position in 3d space that’s just a direction. Unit means it’s between the length is 1, which makes it good for directions.

3 Likes

Why is the unit or the length 1?

Because that’s what Unit means, it’s so you can make the ray any length you want without changing the direction it’s going.

How long would the distance of the ray be if I use mouse.UnitRay?

The length is always 1 i.e. print(mouse.UnitRay.Direction.magnitude) will always print 1. It’s mainly just to get the direction from the camera to the mouse in 3d space.

What do you mean by where the camera is facing? Do you mean the mouse

No, the ray originates from the camera and points from the camera to the mouse in 3d space. Here’s the docs on it:

The UnitRay property is a Ray directed toward the Mouse ’s position in 3D space (described by Mouse.Hit ). It originates from the CFrame of the Workspace.CurrentCamera . Like all unit rays, it has a distance of 1.

https://developer.roblox.com/en-us/api-reference/property/Mouse/UnitRay

1 Like

So UnitRay is basically casting a ray from the camera origin to wherever the mouse has hit? And to get the length of the ray you do Mouse.UnitRay.Direction.magnitude?

My little contribution, imagine that it is a CFrame that is born from the position of the camera and looks towards the mouse:

https://gyazo.com/cf41216833194fea43845b02c71e6984

Code
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local C = Instance.new("Part",workspace)
C.Anchored = true
C.CanCollide = false
C.Size = Vector3.new(1, 1, 2)

while wait() do
    local O = Mouse.UnitRay.Origin
    C.CFrame = CFrame.lookAt(O, O + Mouse.UnitRay.Direction)
end

The numbers in CFrame.LookVector will always be between -1 and 1.

2 Likes

What do you mean by towards the mouse? The mouse look vector? or where the mouse is on there screen

Yes, Direction is where you point from the camera to the mouse, but it is not your position in space 3D.

Look at it like this, the second value of CFrame.LookAt is where it looks, but if you see its CFrame.LookVector, its numbers are always between -1 and 1, RayUnit is the same, but transformed in ray.

Mouse.UnitRay.Origin gets the origin of the mouse on the players screen? Sorry if I am asking the same questions I don’t get what you mean by camera

Maybe:

Player view:

If you transform the camera into a part:

Mouse.UnitRay.Origin is the position of the part in image 2

1 Like

Why would people use this in FPS games? I understand the pictures

To aim the gun, I guess.
Also to know if there is something in front, that’s its use.

In the first person obviously

So Unit Rays only work if you are making the game in first person but in 3rd person you use Ray.new() ?

I guess. I don’t use it, it only occurs to me in the first person.
Raycast would use it for other things but without Mouse.UnitRay

So UnitRay is to position the gun not the bullet or both?

I will figure out how to use unitray tomorrow thanks for the help guys