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!
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.