Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Lock camera to a part and then have it follow a player?

Asked by 6 years ago
Edited 6 years ago

Ive been reading all over the wiki and trying to figure this out because it has to be possible.

I want the camera to lock to a part, and then basically rotate around the part according to where the player is. Do you know what I mean? Like I want the camera to follow the player but stay locked to the part.

So ive tried 3 different scripts and they all do different things but not the thing I want them to do.

Script 1 (This script doesnt work at all really)

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local OFFSET = Vector3.new(40,40,40) -- Position of the camera
local FIELD_OF_IEW = 40

local player = Players.LocalPlayer
local camera = game.Workspace.CurrentCamera

------------------------------------------------

camera.FieldOfView = FIELD_OF_VIEW

local function onRenderStep()
    local character = player.Character
    if character then
        local humanoidRootPart = player:FindFirstChild("HumanoidRootPart")
        if humanoidRootPart then
            local playerPosition = humanoidRootPart.Position
            local cameraPosition = playerPosition + OFFSET
            camera.CoordinateFrame = CFrame.new(cameraPosition, playerPosition)
        end
    end
end

RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onRenderStep)

Script 2 (This locks the camera to the player but not to another part)

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer

repeat
  wait()
until Player.Character

local Character = Player.Character
local Player_Torso = Character.UpperTorso

-------------------------------------------------------------------

Camera.CameraType = Enum.CameraType.Scriptable
Camera.CameraSubject = Player_Torso

game:GetService("RunService").RenderStepped:connect(function()
    if Player.Character and Player.Character:FindFirstChild('UpperTorso') then
    Camera.CFrame = CFrame.new(Player_Torso.CFrame.X, Player_Torso.CFrame.Y + 2, Player_Torso.CFrame.Z + 14)
    end
end)


Script 3 (This locks the camera to a part but then the camera doesnt follow the player)

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Lock = game.Workspace.CameraLock

-------------------------------------------------------------------

Camera.CameraType = Enum.CameraType.Scriptable

game:GetService("RunService").RenderStepped:connect(function()
    if Player.Character and Player.Character:FindFirstChild('Humanoid') then
    Camera.CameraSubject = Lock
    Camera.CFrame = CFrame.new(Lock.CFrame.X, Lock.CFrame.Y + 5, Lock.CFrame.Z + 20)
    end
end)


Thank you for youre time if you want some more info please do ask.

I also just recently tried this one but it doesnt work either.. I think I am getting close

local Camera = game.Workspace.CurrentCamera
local Lock = game.Workspace.CameraLock

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Player_Head = Character.UpperTorso

-------------------------------------------------------------------

Camera.CameraType = Enum.CameraType.Scriptable

game:GetService("RunService").RenderStepped:connect(function()
    if Player.Character and Player.Character:FindFirstChild('Humanoid') then
    Camera.CameraSubject = Lock
    Camera.Focus = CFrame.new(Player_Head.CFrame.X + 0, Player_Head.CFrame.Y + 0, Player_Head.CFrame.Z + 0)
    Camera.CFrame = CFrame.new(Lock.CFrame.X, Lock.CFrame.Y + 5, Lock.CFrame.Z + 20)
    end
end)


1 answer

Log in to vote
0
Answered by
kazeks123 195
6 years ago

Why don't you try this:

camera.CFrame = CFrame.new(part.Position, player.Position) -- should create a CFrame that's position is the part's position, but is looking at player's position (replace player with any of its body parts)

?

Theoretically it should work, unless the camera doesn't accept the new CFrame. In your case it should be something like:

Camera.CFrame = CFrame.new(Lock.Position, Player.Character.Head.Position)
0
That does lock the position to the head but it doesnt keep it locked to the part. GottaHaveAFunTime 218 — 6y
0
Wait nope it works, thanks dude :) i had to do some modifications but it works thanks :) GottaHaveAFunTime 218 — 6y
0
I don't get what you mean by "locked", since as I explained it will put the camera at the position of the part and it will be looking at the player. Isn't that what you wanted? Just put it in a render loop (RenderStepped) and make sure you update the position of the part and player! kazeks123 195 — 6y
Ad

Answer this question