I made a Lock-On Script for ROBLOX that locks onto a specific enemy, but follows the player from a shoulder-surfing view point.
The problem is, when approaching the said enemy or entity that is locked on, and jumping on their head or above them; causes the player to spin when move only forward.
I'm aware that the problem comes from the movement system going towards where the Camera is looking, which is what I want; however, preferably with a seamless transition when moving above the enemy.
-- Declare variables
local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
local runService = game:GetService("RunService")
local dummy = workspace.Dummy
local target = workspace.Lock
camera.CameraType = Enum.CameraType.Scriptable
camera.CameraSubject = target
local angle = 0
-- Wait a frame for camera to load
wait()
-- Update camera on RenderStepped to get smooth motion
runService.RenderStepped:connect(function()
local character = player.Character
-- Check if character and torso exist (in case character is dead)
if character and character.Torso and character.Head then
local torso = character.Torso
local head = character.Head
local ZPosition = Vector3.new(head.CFrame.X, head.CFrame.Y + 2, head.CFrame.Z)
local ZCFrame = CFrame.new(head.CFrame.X, 1, head.CFrame.Z)
local XPosition = Vector3.new(head.CFrame.X + 5, head.CFrame.Y + 2, head.CFrame.Z)
camera.CFrame = CFrame.new(ZPosition, target.Position) * CFrame.new(2, 0, 7)
camera.Focus = CFrame.new(target.Position)
end
end)