1
votes

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)

MP4/GIF

1

1 Answers

1
votes

The camera spinning when on top of the entity's head could be because you're trying to reach a point that you're vertically on top of.

To help you imagine this, visualize if you were trying to use a compass that points towards the north pole to reach the north pole, and you can only walk in the direction the compass is pointing. Once you got near the north pole, the compass would point towards it. But, if you kept walking forwards, eventually you'd walk OVER the north pole and pass over it. Then the compass would spin around and point backwards, so you'd walk backwards. Eventually you'd walk over top of it again and the compass would flip around again.

Eventually you'd get to a point where you're going back and forth over top of it very fast but you never get QUITE directly over top of it. Because the compass always points towards the north pole, you would see the compass spinning around very quickly because you're moving around it very quickly very close to it, but never exactly over top. The closer you get, the smaller movements make a bigger impact on the direction of the compass.

I think this is what's happening, where the camera is locked onto the target and the player is walking forwards. Once you get ON TOP of the target, the camera tries to keep pointing towards the target, but since you're so close, you pass back and forth over the point on the target the camera is pointing to and it whips around trying to point to it.

Maybe you could insert a check like this:

if (distanceToTarget.X >= 1 meter) then
    camera.pointAtTarget(); --Point at the locked on target as your script would
else
    camera.pointNormally(); --Point straight ahead as it normally would if there was no locked target
end

The distance is up to you to determine but the general idea is that if you get close enough to the target that the camera starts whipping all around, it will stop tracking and function normally. Alternatively, if the issue only occurs when ABOVE the target, then try including another condition to check if the player is directly above the target and to stop tracking then.