Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to properly get Mouse.Hit.p for camera manipulation w/ tools?

This code makes a gun move with the player's camera.

Basically my problem is that in low-roofed buildings the camera doesn't move properly because the Y value of mouse.hit.p is not great enough to render a difference. The same goes for when you look down towards the ground. It barely budges!

Here's my code: (mouse is already marked as the player's mouse in the equip function RAni and LAni are the arm's welds respectively.)


RAni = p.Character.Torso["RightAni"]
CF = RAni.C1
LAni = p.Character.Torso["LeftAni"]
CF2 = LAni.C1

mouse.Move:connect(function()
	if script.Parent.Firing.Value then wait() return end
	if script.Parent.Reloading.Value then wait() return end
		
if p.Character:FindFirstChild(script.Parent.Name)~= nil then
local dir = mouse.Hit.p.Unit
local pitch = math.asin(dir.Y)
RAni.C1 = CF * CFrame.Angles(-pitch,0,0)
LAni.C1 = CF2 * CFrame.Angles(-pitch,0,0)
end
end)

I'd appreciate help, or a better algorithm if you have one. Thanks!

1 answer

The hit position of the mouse is the position in the world that the mouse is hovering.

Unless you are assigning special importance to the exact position of the origin, Hit.p.unit should have no meaning.

To find the direction toward the mouse, you have to take (mouse.Hit.p - from).unit where from would probably be the position of the tool or of the player.

This discussion is preserved as it was. New answers, comments, and votes are closed.