Hi, so I have this lock on script, and I’d like for players to be able to zoom in or out while locked on, basically it focuses your camera onto the target, but you cannot zoom in or out and I’d like for that to be possible. Thanks.
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
--Folders
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
--Instances
local Camera = workspace.CurrentCamera
local SetLockedRemote = Remotes.SetLockedOn
local Player = Players.LocalPlayer
local Character = Player.Character
--Values
local Mouse = Player:GetMouse()
local Target = nil
local Range = 999999
--Create a Billboard GUI
local Billboard = Instance.new("BillboardGui")
Billboard.Size = UDim2.new(6,0,6,0)
Billboard.AlwaysOnTop = true
--Add an ImageLabel to the BillboardGUI that serves as a lock on market.
local ImageLabel = Instance.new("ImageLabel", Billboard)
ImageLabel.Image = "rbxthumb://type=Asset&id=7202706401&w=420&h=420"
ImageLabel.BackgroundTransparency = 1
ImageLabel.Size = UDim2.new(1,0,1,0)
--onInputBegan function
local function onInputBegan(input, gameProcessed)
if gameProcessed then return end
--If input is MB3 or L.
if input.UserInputType == Enum.UserInputType.MouseButton3 or input.KeyCode == Enum.KeyCode.L then
--If the target is not set, raycast from Head to MouseHit.
if not Target then
local MouseHit = Mouse.Hit
local rayOrigin = Character.Head.Position
local rayDirection = ((MouseHit.p - rayOrigin).Unit) * Range
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
--SetTarget function.
local function setTarget(newTarget)
--Set camera.
Camera.CameraType = "Scriptable"
--Set target, set the Billboard GUI's Parent and fire the Lock On remote.
Target = newTarget
Billboard.Parent = Target.PrimaryPart
SetLockedRemote:FireServer(newTarget.Name)
wait()
spawn(function()
while Target and Target:IsA("Model") do
local X = 12
local PartPosition = Target.HumanoidRootPart.Position
local SelfPosition = Character.HumanoidRootPart.Position
local Offset = Vector3.new(5,2,0)
local V = PartPosition + ((SelfPosition + Offset) - PartPosition).Unit * ((SelfPosition - PartPosition).Magnitude + X)
Camera.CFrame = CFrame.new(V, PartPosition)
Character.PrimaryPart.CFrame = CFrame.lookAt(Character.PrimaryPart.Position, newTarget.PrimaryPart.Position)
RunService.RenderStepped:Wait()
end
end)
end
if raycastResult then
--If the raycast result is a player or a dummy, lock onto them.
local hitPart = raycastResult.Instance
if hitPart.Parent.Parent == workspace.Dummies and hitPart.Parent:FindFirstChild("Humanoid") then
setTarget(hitPart.Parent)
elseif hitPart.Parent.Parent == workspace and hitPart.Parent:FindFirstChild("Humanoid") then
setTarget(hitPart.Parent)
end
end
--If target exists then unlock.
elseif Target then
Target = nil
Billboard.Parent = nil
Camera.CameraType = "Custom"
SetLockedRemote:FireServer(false)
end
end
end
--Detect userinput.
UserInputService.InputBegan:Connect(onInputBegan)
Players.PlayerRemoving:Connect(function(plr)
--When player leaves, unlock.
if Target ~= nil then
if plr.Name == Target.Name then
Target = nil
Billboard.Parent = nil
Camera.CameraType = "Custom"
SetLockedRemote:FireServer(false)
end
end
end)