I wish to achieve the gun pointing towards the mouse pointer by going left/right
It does do what I want if I point down, but does not take into account distance and spreads out if the player looks too far up. (see the right pistol)
Solutions I have tried so far:
- Using X and/or Z instead of Y
spreads out when pointing in axis directions, ignores height - CFrame.lookat()
breaks from the local CFrame’s X and Z breaking immersion of holding a gun correctly
Here is the part of the script shown in the video
-- reference variables used in the example
local rightHand = character:FindFirstChild("Right Arm") or character:FindFirstChild("RightHand")
local rightGrip = rightHand:FindFirstChild("RightGriprWeld_local", .1)
local mousePosition = mouse.Hit.Position
local distance = (Camera.CFrame.Position - mousePosition).magnitude
-- the part I need help with (ran on renderstepped)
local answer = math.asin((Camera.CFrame.Position.Y - mousePosition.Y) / distance)
rightGrip.C0 = CFrame.new(0, -1, 0) * CFrame.Angles(math.rad(-90), answer, math.rad(0))
I know my current code is the incorrect way to solve this.
Any math or CFrame stuff that might solve this is appreciated, I plan to implement it with left grips later.
And if possible, a simplified explanation of how your solution works would help me a lot.