So i’m adding a abilities to my game and i’m trying to add aimlock. I want it to be a function where I can activate it (locally) kinda like this:
function ActivateAimlock (duration <length that aimlock is activated>)
while wait(duration) do
for i,v in pairs(game.Players) do
if v.Character.Magnitude <= 10 then -- I want it closest to mouse cursor
Aimlock(v.Character.HumanoidRootPart)
end
end
end
end
I tried but failed so i’m asking here. I’m just looking for a way to make the player Camera look at a object (through a function) to simulate aimlock.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Aiming = false
function AimLock()
local target
local lastmagnitude = math.huge --just for the start
for i,v in pairs(game.Players:GetPlayers()) do
if v ~= player then
if v.Character then
local charpos = v.Character.PrimaryPart.CFrame.Position
local playerpos = player.Character.PrimaryPart.CFrame.Position
if (charpos - playerpos).Magnitude < lastmagnitude then
lastmagnitude = (charpos - playerpos).Magnitude
target = v
end
end
end
end
if target and target.Character then
local charpos = target.Character.PrimaryPart.CFrame.Position
local cam = workspace.CurrentCamera
local pos = cam.CFrame.Position
workspace.CurrentCamera.CFrame = CFrame.new(pos, charpos) --I changed the CFrame right here.
end
end
mouse.Button2Down:Connect(function()
Aiming = true
while Aiming do
AimLock()
wait()
end
end)
mouse.Button2Down:Conenct(function()
Aiming = false
end)
So to summarize it, set the position of the camera as the position of the camera (to not glitch it) and the rotation to where the enemies player’s position is (I added it so it would locate the closest enemy). If you need to understand better, I will mark where I changed the CFrame of the camera.
EDIT: I started writing this before I saw @sniper74will 's reply.
It’s possible, but would not necessarily consider this trivial.
First, you must set the Camera.CameraType to "Scriptable". Once you do this, you have complete control over the Camera, which means you must now code the behavior of the Camera manually. You can change it back to "Custom" once you are done with your lock mode.
Next, you must figure out your subject. Who do you want to target? You want to get their Player.Character model and then from there, save a reference to one of their body parts so you can use below. Looks like you want the player closest to your Mouse. You can use Mouse.Target.p to get the position of the Mouse in 3D space, then do some math to get the closest one to your Mouse.
BTW, you can get the mouse using game.Players.LocalPlayer:GetMouse().
Next, you have to do some CFrame math. You want to set the Camera.CFrame to the Position of your character, rotate the camera in the correct direction, then cframe again to offset the camera to where it’s above and behind your head. All of this should be done in a while loop or, more preferably, a RunService.RenderStepped event.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Aiming = false
function AimLock()
local target
local lastmagnitude = math.huge --just for the start
for i,v in pairs(game.Players:GetPlayers()) do
if v ~= player then
if v.Character then
local charpos = v.Character.PrimaryPart.CFrame.Position
local mousepos = mouse.Hit.p
if (charpos - mousepos).Magnitude < lastmagnitude then
lastmagnitude = (charpos - mousepos).Magnitude
target = v
end
end
end
end
if target and target.Character then
local charpos = target.Character.PrimaryPart.CFrame.Position
local cam = workspace.CurrentCamera
local pos = cam.CFrame.Position
workspace.CurrentCamera.CFrame = CFrame.new(pos, charpos) --I changed the CFrame right here.
end
end
mouse.Button2Down:Connect(function()
Aiming = true
while Aiming do
AimLock()
wait()
end
end)
mouse.Button2Down:Conenct(function()
Aiming = false
end)