Help with scripting Camera Lock on to Character System

Hi there! :slight_smile:

  1. What do you want to achieve? I am working on my battle system and want to create a lock on system so that players can lock onto a target and not worry about moving their camera (Like in SoulShatters)

  2. What is the issue? Right now i have the lock system working for the character but with the method I have in place the camera gets unlocked from the character and the character kind of runs off the camera.

  3. What solutions have you tried so far? I have tried looking into magnitude from my target, all over google, and the devforum.

Here is the code I have right now.

local Cam = workspace.CurrentCamera
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

wait(1)

local player = Players.LocalPlayer
local Character = player.Character

repeat
	workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
until workspace.CurrentCamera.CameraType == Enum.CameraType.Scriptable

local function UpdateCam()
	
	local CFam = CFrame.new(script.Parent.Parent.Character.PrimaryPart.CFrame.Position, workspace.Dummy.PrimaryPart.CFrame.Position)
	
	local CamCFam = CFrame.new(
		Vector3.new(script.Parent.Parent.Character.PrimaryPart.CFrame.Position.X - 5, -- X
			script.Parent.Parent.Character.PrimaryPart.CFrame.Position.Y + 3, -- Y
			script.Parent.Parent.Character.PrimaryPart.CFrame.Position.Z - 5), -- Z
		 	workspace.Dummy.PrimaryPart.CFrame.Position -- Lookat
	)
	
	workspace.CurrentCamera.CFrame = CamCFam
	script.Parent.Parent.Character:SetPrimaryPartCFrame(CFam)
	
end

RunService.Stepped:Connect(UpdateCam)

It works with no and as expected until i start moving and changing my Z axis coordinates :confused:

Here is how i want it to look


Here is a video showing how the Character just goes out of frame
robloxapp-20200815-1607438.wmv (3.7 MB) (Sorry about the file type)

If there is some sort of way to do the

script.Parent.Parent.Character.PrimaryPart.CFrame.Position.X - 5, -- X
script.Parent.Parent.Character.PrimaryPart.CFrame.Position.Y + 3, -- Y
script.Parent.Parent.Character.PrimaryPart.CFrame.Position.Z - 5 -- Z

without the X and Z drifting from the character that would be great!
Thanks :smile:

PS: yes i do know i am using CFrame.new instead of CFrame.FromMatrix (I am planning on changing that when i have a working test)

8 Likes

The reason to the strange behavior was that the offset between the camera and the HumanoidRootPart was set in world coordinates, which means the relative offset changed all the time. It was easy to fix that by using CFrame multiplication instead. I also noticed that when the character jumped, your original function tried to rotate it on any axis to make it face towards the dummy. This probably wasn’t something you wanted, and when mixed with humanoid behavior, jumping near the dummy looked weird. I changed it so that the HumanoidRootPart only rotates on the y-axis (only horizontal rotation).

local Cam = workspace.CurrentCamera
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

wait(1)

local player = Players.LocalPlayer
local Character = player.Character

repeat
	workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
until workspace.CurrentCamera.CameraType == Enum.CameraType.Scriptable

local function UpdateCam()
	
	-- CFam changes were done to prevent the character from rotating on the x-axis of the HumanoidRootPart when jumping
	local hrpPos, dummyPos = script.Parent.Parent.Character.PrimaryPart.CFrame.Position, workspace.Dummy.PrimaryPart.Position
	local CFam = CFrame.new(hrpPos, Vector3.new(dummyPos.X, hrpPos.Y, dummyPos.Z))
	
	-- making the offset relative to the HumanoidRootPart. It was in world coordinates before.
	local CamCFam = CFam*CFrame.new(3, 3, 7) -- change these numbers to change the offset
	
	workspace.CurrentCamera.CFrame = CamCFam
	script.Parent.Parent.Character:SetPrimaryPartCFrame(CFam)
	
end

RunService.RenderStepped:Connect(UpdateCam)
7 Likes

You’re a lifesaver! I was going down the route of LookVectors and wasn’t going to find success anytime soon. Thank you :smiley:

Hey, can you help me? I need a Lock-On Script similar to Soul Shatters too.

2 Likes