PcoWSkbVqDnWTu_dm2ix
Collapse Sidebar

Hit

Mouse

CFrame

This property indicates DataType/CFrame of the mouse’s position in 3D space. Note that 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 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 part).

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

local unitRayDirection = mouse.UnitRay.Direction
local mouseHitDirection = mouse.Hit.lookVector
-- unitRayDirection ≈ mouseHitDirection
-- the vectors are approximately 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), this property 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.


Mouse Origin vs Mouse Hit vs CurrentCamera Position

The code below visualizes the difference between Mouse/Hit and Mouse/Origin. In order to do this, the code uses the DataType/Vector3 positions of the hit and origin DataType/CFrame values using .p.

The difference is that the origin is “where the mouse came from” (its origin) and the hit is the position where the mouse hits (is when the player presses their mouse).

This example also visualizes that the mouse origin is very similar to the position of the CurrentCamera by printing the magnitude (distance) between the two positions.