Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 8 years ago

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?

0
CFrame needs 3 arguments and the script has to be a LocalScript. That's all I could see for now. Wutras 294 — 8y
0
A CFrame can be constructed with two Vector3s. XAXA 1569 — 8y

1 answer

Log in to vote
1
Answered by
XAXA 1569 Moderation Voter
8 years ago

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()

0
Thanks, it seems to work almost perfectly. MisaMiner 50 — 8y
Ad

Answer this question