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

Why won't the gun point where the mouse is located?

Using mouse.hit, I attempted to make a player's arm always point to the mouse. This is my result.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Active = false


script.Parent.Equipped:connect(function()
	Active = true
	repeat 
		Player.Character.Torso["Right Shoulder"].C0 = CFrame.new(Player.Character.Torso["Right Shoulder"].C0.p, Mouse.Hit.p)
		game:GetService("RunService").RenderStepped:wait()
	until Active == false
end)

Now, the issue with this is that it doesn't point towards the mouse. I have tried other methods, such as getting the components, but so far nothing has worked the way I want it to. Any ideas as to why?

2 comments

0

CFrame needs 3 arguments and the script has to be a LocalScript. That's all I could see for now.

0

A CFrame can be constructed with two Vector3s.

1 answer

✓ Accepted answer

You seem to be going about it incorrectly. Make a CFrame that starts at the shoulder, looking at the mouse (in world coordinates). Then, "subtract" it by the shoulder's CFrame in world coordinates. Afterwards, rotate it so it points at the right direction. Lastly, offset it by half an arm's length forward (y-axis).

repeat 
    -- the shoulder's CFrame, in world coords (??).
    local shoulder = Player.Character.Torso.CFrame * CFrame.new(1, 0.5, 0)
    -- the CFrame where the mouse hit
    local lookat = Mouse.Hit
    -- creates a cframe starting at the shoulder, looking at where the mouse hit
    local newC0 = CFrame.new(shoulder.p, lookat.p)
    -- multiplies the cframe by the inverse of the shoulder's cframe.
    newC0 = Player.Character.Torso.CFrame:inverse() * newC0
    -- rotates the arm so that it points in the right direction
    newC0 = newC0 * CFrame.Angles(math.pi/2, 0, -math.pi/2)
    -- moves it by one stud towards where the arm is pointing
    newC0 = newC0 * CFrame.new(1, 0, 0)
    -- sets the shoulder's CFrame
    Player.Character.Torso["Right Shoulder"].C0 = newC0
    game:GetService("RunService").RenderStepped:wait()
until Active == false

By the way, I would recommend binding this to RenderStepped instead of using RenderStepped:wait()

1 comment

0

Thanks, it seems to work almost perfectly.

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