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 7 years ago
Edited 7 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)

01local RunService = game:GetService("RunService")
02local Players = game:GetService("Players")
03 
04local OFFSET = Vector3.new(40,40,40) -- Position of the camera
05local FIELD_OF_IEW = 40
06 
07local player = Players.LocalPlayer
08local camera = game.Workspace.CurrentCamera
09 
10------------------------------------------------
11 
12camera.FieldOfView = FIELD_OF_VIEW
13 
14local function onRenderStep()
15    local character = player.Character
View all 26 lines...

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

01local Camera = game.Workspace.CurrentCamera
02local Player = game.Players.LocalPlayer
03 
04repeat
05  wait()
06until Player.Character
07 
08local Character = Player.Character
09local Player_Torso = Character.UpperTorso
10 
11-------------------------------------------------------------------
12 
13Camera.CameraType = Enum.CameraType.Scriptable
14Camera.CameraSubject = Player_Torso
15 
16game:GetService("RunService").RenderStepped:connect(function()
17    if Player.Character and Player.Character:FindFirstChild('UpperTorso') then
18    Camera.CFrame = CFrame.new(Player_Torso.CFrame.X, Player_Torso.CFrame.Y + 2, Player_Torso.CFrame.Z + 14)
19    end
20end)

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

01local Camera = game.Workspace.CurrentCamera
02local Player = game.Players.LocalPlayer
03local Lock = game.Workspace.CameraLock
04 
05-------------------------------------------------------------------
06 
07Camera.CameraType = Enum.CameraType.Scriptable
08 
09game:GetService("RunService").RenderStepped:connect(function()
10    if Player.Character and Player.Character:FindFirstChild('Humanoid') then
11    Camera.CameraSubject = Lock
12    Camera.CFrame = CFrame.new(Lock.CFrame.X, Lock.CFrame.Y + 5, Lock.CFrame.Z + 20)
13    end
14end)

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

01local Camera = game.Workspace.CurrentCamera
02local Lock = game.Workspace.CameraLock
03 
04local Player = game.Players.LocalPlayer
05local Character = Player.Character
06local Player_Head = Character.UpperTorso
07 
08-------------------------------------------------------------------
09 
10Camera.CameraType = Enum.CameraType.Scriptable
11 
12game:GetService("RunService").RenderStepped:connect(function()
13    if Player.Character and Player.Character:FindFirstChild('Humanoid') then
14    Camera.CameraSubject = Lock
15    Camera.Focus = CFrame.new(Player_Head.CFrame.X + 0, Player_Head.CFrame.Y + 0, Player_Head.CFrame.Z + 0)
16    Camera.CFrame = CFrame.new(Lock.CFrame.X, Lock.CFrame.Y + 5, Lock.CFrame.Z + 20)
17    end
18end)

1 answer

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

Why don't you try this:

1camera.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:

1Camera.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 — 7y
0
Wait nope it works, thanks dude :) i had to do some modifications but it works thanks :) GottaHaveAFunTime 218 — 7y
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 — 7y
Ad

Answer this question