How to retain normal camera behaviour on z and y axis while locking x for lockon system

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Hello. I made a camera lockon system. I made a scriptable camera and used a lookat Cframe. I’ve got this far but now I’m stumped. I want the z and y axis to behave like the regular camera. only the x axis should be locked. I think i’m saying that righ. The behaviour I want is for the player to be able to zoom out and move their camera up and down like normal. But for the camera to turn with the enemy when locked on.

  1. What is the issue? Include screenshots / videos if possible!

I don’t know how to do this. I tried to feed it the original camera position. Don’t know if i made a new camera by declaring it scriptable, don’t know if, if not, any of the old behaviours can be accessed. If it is a new camera, I dont know if i need to rescript the ‘scriptable’ camera to behave like the normal one…? I’m hoping that is not true any of that, and it’s just something about the math that i dont know.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I cannot find a topic on this, though i’ve found helpful articles on the original lockon setup.

I would be very grateful in anyone would look at my script and tell me about any step forward i might not know about.



--//[[Variables]]\\--
--[[Services]]--
local TWEEN_SERVICE = game:GetService("TweenService")

--[[Folders]]
local EVENTS = script.Parent:WaitForChild("Events")

--[[Camera]]--
local Camera = workspace.CurrentCamera

--[[Character]]--
local char = script.Parent
local Head = char:WaitForChild("Head")
local Root = char:WaitForChild("HumanoidRootPart")

local enemytobefaced = nil

local combatData = char:WaitForChild("combatData")

--[[Scripting]]--
local lockedOn = false

--[[Utility]]--
-- FOR THE WHILE LOOP CONTINUOUSLY CHECKING
local function renderWait()
	game:GetService("RunService").RenderStepped:Wait()
	return true
end

--CONDITIONS
local function conditions()
	if not lockedOn then return end
	return true
end

-- Functions


local function lockOnCamera()
	Camera.CameraType = Enum.CameraType.Scriptable
	while renderWait() do
		if not conditions() then
			break
		end
		local lockOnCameraPosition = Vector3.new(Root.Position.X, Root.Position.Y, Root.Position.Z)
		Camera.CFrame = CFrame.lookAt(Root.Position, enemytobefaced.HumanoidRootPart.CFrame.Position) * CFrame.new(1,5,20)
	end

end	

EVENTS.LockOnToggle.Event:Connect(function(target)
	if target then
		lockedOn = true
		enemytobefaced = target
		lockOnCamera()
	else
		lockedOn = false
		Camera.CameraType = Enum.CameraType.Custom
	end
end)


Thank you!!

Yo what have you tried I’m also making a lock on camera system but I have some stuff to improve

Hey, I decided that a constantly updating camera lock on wasn’t necessary for what I wanted to achieve. Even counter productive really. I stuck with the character turning towards the enemy but the camera only turns the first time you lock on and after a roll. I was working on perfecting that before I got distracted. I’ll go back to it and if you want I can show you the code

Camera.CFrame = CFrame.lookAt(Camera.CFrame.Position, enemytobefaced.HumanoidRootPart.CFrame.Position) * CFrame.new(1,5,20)