2D Lock-on Help

I’m creating a fighting game, and I have created a basic 2D camera system. However, the camera is stationary; and I’d like for the camera to move with the opponent. Something like this clip:

not mine, from one of @tentergram posts’


However I have no idea how you could tackle this, I was thinking something like CFrame:Lerp. How would you advise doing something like the clip provided?
My current code:

local player = game:GetService("Players").LocalPlayer
local camera = workspace.CurrentCamera
local runservice = game:GetService("RunService")

local character = player.Character or player.CharacterAdded:Wait()
camera.CameraType = Enum.CameraType.Scriptable
camera.FieldOfView = 47.5
local HRP = character:WaitForChild("HumanoidRootPart")

runservice.RenderStepped:Connect(function()
	local characterPos = HRP.Position
	camera.CFrame = CFrame.new(characterPos) * CFrame.new(0,0,30)
end)

You could put an ObjectValue named “Target” into the player with the PlayerAdded event. Then when they start attacking something, set the value of that ObjectValue to the Model of the character that they are attacking. When they are done attacking, set the ObjectValue to nil. Then you could do something like this:

local objectValue = player:WaitForChild("Target")

runservice.RenderStepped:Connect(function()
        local characterPos
        if objectValue ~= nil then --check if they are attacking something
             characterPos = objectValue.Value.PrimaryPart.Position --set camera to what they are attacking
      else --check if they aren't attacking something
	     characterPos = HRP.Position --set camera to the player's character
      end
        camera.CFrame = CFrame.new(characterPos) * CFrame.new(0,0,30)
end)
1 Like