PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Hit

Mouse

CFrame

The DataType/CFrame of the mouse’s position in 3D space.

Note, Mouse/TargetFilter and its descendants will be ignored.

Developers can get obtain the position of Hit like so:

local position = mouse.Hit.p

Hit is often used by Tools to fire a weapon towards the mouse in third person.

Developers looking for the BasePart the mouse is pointing at should use Mouse/Target.

How is Mouse.Hit calculated?

The position of the Hit DataType/CFrame is calculated as the point of intersection between the mouse’s internal DataType/Ray (an extended version of Mouse/UnitRay) and an object in 3D space (such as a BasePart).

The orientation of the Hit DataType/CFrame corresponds with the direction of the Mouse/UnitRay.

local unitRayDirection = mouse.UnitRay.Direction
local mouseHitDirection = mouse.Hit.lookVector
-- unitRayDirection ≈ mouseHitDirection
-- the vectors are aproximately equal

Note, the roll of the Workspace/CurrentCamera is not used when calculating the orientation of the Hit DataType/CFrame.

The Mouse's internal ray extends for 1000 studs. If the mouse is not pointing at an object in 3D space (for example when pointing at the sky), Mouse.Hit will be 1000 studs away from the Workspace/CurrentCamera.


Code Samples


Mouse.Hit Laser Beam

The code in this sample, when placed inside a LocalScript within StarterPlayerScripts will draw a red laser beam between the character’s head and Mouse/Hit at all times.

Note, this beam will pass directly through obstructions in third person as the Mouse's raycasting is done from the Workspace/CurrentCamera not the head.