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

How do I make it where my gun aims down when the right mouse button is held down?

Asked by 3 years ago

I am trying to make my gun be able to aim down and move the camera a bit closer to the camera when i hold down the right mouse button, the right mouse button work and all but I am unsure on how to make it where the camera zooms a bit closer into the gun while you aim it down. I was thinking something like a shift lock so the player is always aiming at what he needs to shoot at, not sure if its the best idea but that is my guess. It would need to aim in fast and then when the right mouse button is not down go back to the normal player camera. Is there any way I can do this?

Current Code:

Mouse.Button2Up:Connect(function()
    print("Right Mouse up")
end)

Mouse.Button2Down:Connect(function()
    print("Right Mouse down")
end)

A good example is like the 3rd person GTA 5 aim. Something like that when you aim down the sights.

Any help is appreciated, thank you for reading this and possibly trying to help me.

0
Assuming the player is already in Third-Person (the way you want it to be) then take the Camera.CFrame and simply move it forward. radiant_Light203 1166 — 3y
0
I have a script for that, lemme fetch it Cynical_Innovation 595 — 3y

1 answer

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

Here's a script that does that. Works for me and it should work for you.

local RunServ = game:GetService("RunService")
local originalView = Camera.FieldOfView
local zooming = false
local UIS = game:GetService("UserInputService")
local Camera = workspace.CurrentCamera

UIS.InputBegan:Connect(function(input)
        if input.UserInputType == Enum.UserInputType.MouseButton2 then
        if (Camera.CoordinateFrame.p - game.Players.LocalPlayer.Character.Head.Position).magnitude < 1 then --//Makes it so that you can only zoom in in first person, if want to be able to zoom in in third person, remove this.
            zooming = true
            UIS.MouseDeltaSensitivity = 90/originalView --//Changing mouse sensitivity
            for i = 1, 60 do
                if zooming == false then break end
                Camera.FieldOfView = Camera.FieldOfView + (45 - Camera.FieldOfView) * (i / 60) --/We do some math to make the camera zoom in properly. We could have just set the camera FOV, but we do that instead to make it look smoother.
                RunServ.RenderStepped:wait()
            end

            zooming = false
    end end
end)


local inputend = UIS.InputEnded:Connect(function (input, GameHandledEvent)
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        zooming = false
        UIS.MouseDeltaSensitivity = 1 --//Changing mouse sensitivity again
        for i = 1, 60 do
            if zooming == true then break end
            Camera.FieldOfView = Camera.FieldOfView + (originalView - Camera.FieldOfView) * (i / 60) --//Same Thing On this side
            RunServ.RenderStepped:wait()
        end
    end
end)

Let me know if you have any problems. If you don't, then accept my answer! Cheers, Zander.

Ad

Answer this question