PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
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.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

-- grab local player
local localPlayer = Players.LocalPlayer

-- create beam
local beam = Instance.new("Beam")
beam.Segments = 1
beam.Width0 = 0.2
beam.Width1 = 0.2
beam.Color = ColorSequence.new(Color3.new(1, 0, 0))
beam.FaceCamera = true

-- create attachments
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
beam.Attachment0 = attachment0
beam.Attachment1 = attachment1

-- parent attachments to Terrain 
beam.Parent = workspace.Terrain
attachment0.Parent = workspace.Terrain
attachment1.Parent = workspace.Terrain

-- grab the mouse
local mouse = localPlayer:GetMouse()

-- connect to RenderStepped (update every frame)
RunService.RenderStepped:Connect(function()

	-- make sure the character exists
	local character = localPlayer.Character
	if not character then
		-- disable the beam
		beam.Enabled = false
		return
	end

	-- make sure the head exists
	local head = character:FindFirstChild("Head")
	if not head then
		-- disable the beam
		beam.Enabled = false
		return
	end

	-- enable the beam
	beam.Enabled = true

	-- define origin and finish
	local origin = head.Position
	local finish = mouse.Hit.p

	-- move the attachments 
	attachment0.Position = origin
	attachment1.Position = finish
end)



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.

local mouse = game.Players.LocalPlayer:GetMouse()
local camPos = game.Workspace.CurrentCamera.CoordinateFrame.p

print("-------------------------------------------------")
print("Mouse.Hit:\t|",  mouse.hit.p)
print("camPos:\t|", camPos)
print("Mouse.Origin:\t|", mouse.Origin.p)
print("-------------------------------------------------")
print("Magnitude:\t|",(mouse.Origin.p - camPos).magnitude)
print("-------------------------------------------------")