I'm making a sniper rifle, and I am going to make the camera zoom in when I click RMB. At first I thought I could just use FoV but from what I've read that won't work. Is there a method in camera to make it zoom in? Or can I just use Fov?
If you're making a sniper rifle, Using FoV isn't the best idea: FoV means Feild of View. Lowering it will make your view more narrow, causing you too see less. It's best to change the position of the camera, and maybe slightly changing the field of view.
So first, we want to detect the Right Mouse Button input and define the camera.
cam = game.Workspace.CurrentCamera --Assuming this is in a local script mouse = game.Players.LocalPlayer:GetMouse() --Assuming this is in a local script mouse.Button2Down:connect(function() --Right Click end)
This step is for if you have animations Next we want to move to the ADS, a good way to do this is to store all of your keyframes in a module script and call them in the main script.
local animModule = require(game.ReplicatedStorage.Anim) animModule.AdsIn() -- Function name for the animation
Now we want to change the camera position to the player's position + a small value
cam.CoordinateFrame = cam.CoordinateFrame*CFrame.new(0,0,5) --If you can see through walls, a Vector3 might be better
That was simple, Right?
Now if you have a scope GUI you want that to be visible.
script.gunGui.Scope.Visible = true
Now you can scope with Right Mouse!
But you're also stuck!
So now we need to return!
To do this we start with a buttonup function.
mouse.Button2Up:connect(function() --Right Release end)
Next we return the camera to it's default state.
cam.CoordinateFrame = cam.CoordinateFrame*CFrame.new(0,0,-5)
And we make the scope GUI invisible.
script.gunGui.Scope.Visible = false
Then call the animation if you have any
local animModule = require(game.ReplicatedStorage.Anim) animModule.AdsOut() -- Function name for the animation
And now you're done! The final result should look like this:
cam = game.Workspace.CurrentCamera mouse = game.Players.LocalPlayer:GetMouse() mouse.Button2Down:connect(function() local animModule = require(game.ReplicatedStorage.Anim) animModule.AdsIn() cam.CoordinateFrame = cam.CoordinateFrame*CFrame.new(0,0,-5) script.gunGui.Scope.Visible = true end) mouse.Button2Up:connect(function() cam.CoordinateFrame = cam.CoordinateFrame*CFrame.new(0,0,-5) script.gunGui.Scope.Visible = false local animModule = require(game.ReplicatedStorage.Anim) animModule.AdsOut() end)
Note: It might be simpler to define the animModule with cam and mouse, rather than in each function.
Use CameraMaxZoomDistance. Since it only affects one player at a time and you can use a regular script instead of a local one, It's great!
game:GetService("Players").PlayerAdded:connect(function(player) player.CameraMaxZoomDistance = 0.5 --0.5 is the minimum. end)
If you would like to use it in a script:
--Hopperbin, can be regular script or local. script.Parent.Selected:connect(function() local player = game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent) if player then player.CameraMaxZoomDistance = 0.5 --If you change it to 0, it will just turn back to 0.5. end end)
Hope it helps!