Lock-on camera glitches when target is too close

Hey there, I’m currently having an issue with my lock-on script. It works fine; but the issue is that the camera spazzes out when I move in too close to the target.

This is my part of the code:

		spawn(function()
			while Focused do
				local B = nearestPart.Position
				local A = char.HumanoidRootPart.Position
				local Offset = Vector3.new(5,1,0)
				local V = B + ((A + Offset) - B).Unit * ((A - B).Magnitude + 8)
				Cam.CFrame = CFrame.new(V, B)
				UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
				UserInputService.MouseIconEnabled = false
				char.HumanoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.Position, nearestPart.Position)
				run.RenderStepped:Wait()
			end
		end)

I’ve spent hours trying out different stuff but it all would still end up spazzing out in the end.
It would be much appreciated if you could help me with this.

You can try using the CFrame constructor CFrame.new ( Vector3 pos, Vector3 lookAt ), with pos being the cameras position and lookAt being the targets root part. You would simply put this line of code in a render stepped loop:

camera.CFrame = CFrame.new(camera.CFrame.Position, rootPart.Position)
4 Likes